# x%10 then what? [getting digits sum] of a (positive long int)

• 02-07-2005
amirahasanen1
x%10 then what? [getting digits sum] of a (positive long int)
If I have a (long int x) that I want to get the sum of its digits, I know I should use modulus & devision, declare a temp variable as well as one for sum. But I am really confused about the exact equations, I only know how to get the first digit (units) by using:

Code:

```temp = x % 10; sum += temp;```
and I think that I will use loops as well, do I have to?
and what are the equations/code used?

Thanks,
Amy
• 02-07-2005
major_small
well, think of what the modulo operator does - it returns the remainder of a division. so, if you do x%10, you're dividing by 10 and returning what's left (a single digit). what happens when you use 100? 1000?
• 02-07-2005
Hunter2
Well, you've got the first digit; now if you want to keep using the same formula for getting the value of each digit (i.e. x % 10), you'll need to keep moving digits over to the right. You can do this by dividing x by 10 (any decimal part will automatically be cut off). For example:
Code:

```temp = x % 10;  //Get the units digit sum += temp;  //Add to running total x /= 10;  //divide x by 10, store the result in x. This shifts everything to the right one place. temp = x % 10;  //Get the new units digit sum += temp;  //Add to running total x /= 10;  //Shift everything toward the units digit one place //etc. etc. for each digit```
As you can imagine, this can very easily be put into a loop that ends when you've reached the last digit, which I'll leave to you to do.
• 02-07-2005
amirahasanen1
Quote:

Originally Posted by Hunter2
Well, you've got the first digit; now if you want to keep using the same formula for getting the value of each digit (i.e. x % 10), you'll need to keep moving digits over to the right. You can do this by dividing x by 10 (any decimal part will automatically be cut off). For example:
Code:

```temp = x % 10;  //Get the units digit sum += temp;  //Add to running total x /= 10;  //divide x by 10, store the result in x. This shifts everything to the right one place. temp = x % 10;  //Get the new units digit sum += temp;  //Add to running total x /= 10;  //Shift everything toward the units digit one place //etc. etc. for each digit```
As you can imagine, this can very easily be put into a loop that ends when you've reached the last digit, which I'll leave to you to do.

I couldn't know the condition which states that the number ended when using this code.
• 02-07-2005
amirahasanen1
Quote:

Originally Posted by major_small
well, think of what the modulo operator does - it returns the remainder of a division. so, if you do x%10, you're dividing by 10 and returning what's left (a single digit). what happens when you use 100? 1000?

I originally wanted to write a program to input a positive integer and to compute the sum of its digits, then determine whether it is divisible by 9 using the rule (an integer N is divisible by 9 if the sum of its digits is divisible by 9)..

I made it this way and it is working:

Code:

```#include <iostream> using namespace std; int main() {         long int N;         cin>>N;         float temp = 0;         int sum = 0;         long int modulus;         long int division;         temp = N % 10;         sum += temp;         modulus = 100;         division = 10;         while(temp>=0)         {                 temp = N % modulus;                 temp /= division;                 sum += temp;                 modulus *= 10;                 division *= 10;                 temp /= modulus;         }                 if(sum==9)                 cout<<"The number is divisible by 9.";                 cin.ignore();         cin.get();         return 0; }```
Is it a good way??
• 02-07-2005
modulus *= 10;
division *= 10;

temp /= modulus;

leave those lines out. Delete the following and replace them by intializing the values at the time of declaration

modulus = 100;
division = 10;

You modify temp in the while loop so leave out these as well.

temp = N % 10;
sum += temp;

Before you do that however, set the value of temp to something other than 0 and run your program. Analyze the results and see why I made the suggestions.

Oh, and I think you want

if(sum % 9 == 0)

if(sum==9)

to meet the protocol you suggested.
• 02-07-2005
amirahasanen1
Quote:

Before you do that however, set the value of temp to something other than 0 and run your program. Analyze the results and see why I made the suggestions.

Why something other than 0?
• 02-07-2005
Hunter2
>>determine whether it is divisible by 9
Well, the most efficient way to do so would be simply:
Code:

`bool isDivisible = ((theNumber % 9) == 0);`
Anyhow, back to this:
Quote:

I couldn't know the condition which states that the number ended when using this code.
You can tell that the number ended, simply when x == 0. Since you're continually getting the units digit and then truncating it, when x reaches 0 you know that you've run out of digits to sum ;) In this way, you'll replace 10 confusing lines with 3 simple ones.
• 02-07-2005
Scribbler
There are only 3 items you need for this to work...

1st, a sentinel for the loop. You used while ( temp >= 0 ). However I would recommend using simply
Code:

`while ( temp )`
This way it will also work with negative numbers (for whatever reason). As long as Temp is a non-zero value, it will result as true.

2nd, you need to strip the digit and add it to the sum. sum += ( temp % 10 ); will do that.

3rd, remove the digit from the number entirely. temp /= 10;

And there you have a simple while loop with 2 statements...
Code:

```        while ( temp )         {                 sum += ( temp % 10 );                 temp /= 10;         }```
• 02-07-2005
amirahasanen1
Quote:

Originally Posted by Hunter2
>>determine whether it is divisible by 9
Well, the most efficient way to do so would be simply:
Code:

`bool isDivisible = ((theNumber % 9) == 0);`

Yes, I understand this way and I know it is the efficient, but in my program I wanted to determine that using a certain rule, which is: an integer N is divisible by 9 if the sum of its digits is divisible by 9.

Quote:

Anyhow, back to this:
You can tell that the number ended, simply when x == 0. Since you're continually getting the units digit and then truncating it, when x reaches 0 you know that you've run out of digits to sum ;) In this way, you'll replace 10 confusing lines with 3 simple ones.
Oh yea, right. Because I will already keep moving digits over to the right :)
Thanks alot,

-Amy
• 02-08-2005
amirahasanen1
Oh, there is a small problem with the program, if the input (N) exceeds 10 digits the program ends, although N is declared as a long integer.
Does anybody know why this happens?
• 02-08-2005
Hunter2
Yes, the problem is simply that a long integer cannot hold a number that large. You can look the exact boundaries up in a book or on the internet pretty much anywhere (C++ datatype sizes). If you want to hold a larger 'number', you'll have to input it as a string of characters (which, incidentally, makes your algorithm much more intuitive - the number is already broken up into an array of single digits).

My guess as to why it's quitting is that you're using cin.get() to pause your program at the end. However, if you have an error inputting some data (i.e. cin >> theNumber, where the input is to large for theNumber to hold), an error flag will be set in cin and all successive input operations will return immediately with a failure. Thus, the rest of the program flashes by too quickly for you to see and the cin.get() at the end doesn't pause anything.