Sum of digits of an integer: Odd or Even?

This is a discussion on Sum of digits of an integer: Odd or Even? within the C Programming forums, part of the General Programming Boards category; Hey, I've been trying to work out a function for this for a good while, I'm brand new to loops ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    14

    Sum of digits of an integer: Odd or Even?

    Hey, I've been trying to work out a function for this for a good while, I'm brand new to loops so I'm not sure which would be the best to use to separate the digits out to add. I do know from some searching around that I need to do:

    number % 10 to get the digit alone

    number / 10 to strip away the right-most number, I'm just having troubles with looping it.

    Thanks in advance!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    I'm sure any type of loop will do, it doesn't matter.

    >> number / 10 to strip away the right-most number, I'm just having troubles with looping it.
    Think about what that means, though. All positive numbers have a lower bound of zero, so when are you done?

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Would you be done when number < 1?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    If number is always positive, yes.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Could I do something like:
    Code:
    {
    	int number = 0, digit = 0;
    
    	for(number != 0)
    	{
    		digit = number % 10;
    
    		number = number / 10;
    	}
    
    }

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Well, I got the loop figured out, but now I'm having trouble adding the digits of the integer together (eg. 1987; 1+9+8+7). I don't really know how to store or save the digits after they have been singled out. Any help is much appreciated!

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    When you get the first digit, you would add it to the sum of zero. And then start adding more digits until you run out.

    sum = sum + digit

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Ok, thanks for the help! Would this function do the trick?

    Code:
    int isOddEven(int intNumber)
    {
    	int number = 0, digit = 0, sum = 0;
    
    	while(number != 0)
    	{
    		digit = number % 10;
    
    		sum = sum + digit
    
    		number = number / 10;
    	}
    
    	return sum;
    
    }

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Devolution View Post
    Ok, thanks for the help! Would this function do the trick?

    Code:
    int isOddEven(int intNumber)
    {
    	int number = 0, digit = 0, sum = 0;
    
    	while(number != 0)
    	{
    		digit = number % 10;
    
    		sum = sum + digit
    
    		number = number / 10;
    	}
    
    	return sum;
    
    }
    Apart from a missing ";" and the fact that the name of the function is "isOddEven" but returns the sum of the digits, and the fact that you will always return 0 since you're using the local variable number instead of the parameter passed into the function.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Oops, yea I got the function name switched up with another one.

    How do I return the parameter passed into the function? I thought returning sum would do that, but I'm pretty new to C, and things haven't "clicked" for me yet I'm trying my best to understand so I don't fall behind.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Devolution View Post
    Oops, yea I got the function name switched up with another one.

    How do I return the parameter passed into the function? I thought returning sum would do that, but I'm pretty new to C, and things haven't "clicked" for me yet I'm trying my best to understand so I don't fall behind.
    You don't want to return the parameter passed into the function, and I didn't suggest you do so. I suggest you use the parameter passed into the function, instead of completely ignoring it in favor of calculating the sum of the digits of 0 (a/k/a number) instead.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Ok I believe I understand what you're saying now, correct me if I'm wrong though. The "number" variable in there too was another mix-up that I didn't mean to have in the function, but thanks for pointing it out. Fixed up, it looks like this:

    Code:
    int sumDigits(int intNumber)
    {
    	int digit = 0, sum = 0;
    
    	while(intNumber != 0)
    	{
    		digit = intNumber % 10;
    
    		sum = sum + digit;
    
    		intNumber = intNumber / 10;
    	}
    	return sum;
    }

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't really see the point of your function.
    To check if a number is even or odd, you just use the % operator and specify a certain number afterwards, and that number isn't 10.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    We're checking to see if the sum of the digits of an integer are even or odd.

  15. #15
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Devolution View Post
    Ok I believe I understand what you're saying now, correct me if I'm wrong though. The "number" variable in there too was another mix-up that I didn't mean to have in the function, but thanks for pointing it out. Fixed up, it looks like this:

    Code:
    int sumDigits(int intNumber)
    {
    	int digit = 0, sum = 0;
    
    	while(intNumber != 0)
    	{
    		digit = intNumber % 10;
    
    		sum = sum + digit;
    
    		intNumber = intNumber / 10;
    	}
    	return sum;
    }
    instead of "sum = sum + digit;" you can just write "sum += digit;"
    same thing for intNumber "intNumber /= 10;"

    saves a little bit of typing
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 11:53 AM
  2. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  3. Counting integer digits
    By Lionmane in forum C Programming
    Replies: 22
    Last Post: 05-24-2005, 11:11 AM
  4. Replies: 11
    Last Post: 12-24-2002, 11:00 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21