Thread: adding odd numbers

  1. #1
    Unregistered
    Guest

    adding odd numbers

    hi
    if i have a list of numbers , say 2345678
    and want to add up the odd position digits
    ie 2+4+6+8

    how can i do this ?

    i tried it with nums[20]
    and using atoi to convert the type

    but it seems i can't do things like

    sum=atoi(nums[0]) + atoi(nums[1]);

    can someone plz help me~~~~

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Try this:

    PHP Code:
    char nums[]="2345678"
    int i;
    int add=0;
    for(
    i=0strlen(nums); 2)
    {
    add add + (atoi(nums[i]) );   /// OR add += (atoi(nums[i]) )
    }
    printf("%i"add); 
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try changing :-

    for(i=0; i < strlen(nums); i + 2)
    to
    for(i=0; i < strlen(nums); i += 2)
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    this will do it for any number
    Code:
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_SIZE 15
    
    
    int function(int num)
    {
        int ret = 0;
        char number[MAX_SIZE];
        memset(number,'\0',MAX_SIZE);
        _itoa(num,number,10);
        for(int i = 0;number[i] != '\0';i++)
       {
            if((i % 2 != 0) || i == 1)
                ret += i;
       }
       return ret;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Does it have to be a string

    int num[7] ={2,3,4,5,6,7,8};

    then

    for(i =0; i < 7;i++)
    if(i % 2 == 0)
    total += num[i];

    should do the trick
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    zoo
    Guest
    A third way.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int add_odd_digits(int number);
    int main()
    {
       int num = 12345;
       int sum;
       sum = add_odd_digits(num);
       printf("sum of odd digits:%d\n",sum);
       return 0;
    }
    
    int add_odd_digits(int number)
    {
       int digit, odd_sum;
       int sum1 = 0;
       int sum2 = 0;
       while (number > 0)
       {
          digit = number % 10;
          number /= 10;
          sum1 += digit;
          if (number <=0)
             return sum1;
          digit = number % 10;
          number /= 10;
          sum2 += digit;
          if (number <=0)
             return sum2;
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help C program that reads Odd numbers and evens
    By Cyberman86 in forum C Programming
    Replies: 4
    Last Post: 02-27-2009, 11:59 AM
  2. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  3. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  4. adding base n numbers
    By doogle in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 11:23 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM