Thread: Sum of digits of an integer: Odd or Even?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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;
    
    }

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, 10: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, 10: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