A program that sum all digits of entered number can you help

• 05-25-2003
Tonyukuk
A program that sum all digits of entered number can you help
I want to do a program that you can enter an integer than it will sum all of its digits.
example: 189
1+8+9=18

but I have an error.My program does this;
example: 189
189+89+90=368

what should I do? Here is my code

<code>
int counter=0,sum=0;
char n[100];
cin>>n;
while(n[counter]!=NULL){
counter++;
}

for(int i=0;i<=counter;i++){

sum=atoi(&n[i])+sum;
cout<<(&n[i])<<endl;
}
cout<<sum<<endl;
</code>
• 05-25-2003
Wledge
Try this
Code:

```        char *str = "189";         int sum = 0;         for(int i = 0;str[i];i++)                 sum += str[i] - '0';         cout << sum << endl;```
• 05-25-2003
Tonyukuk
thanks a lot. but what is '0' NULL or something?
• 05-25-2003
Magos
If you enter it as a string, loop though all elements and sum them up:
Code:

```char Buffer[SIZE]; int Sum = 0; for(int i = 0; i < strlen(Buffer); i++) {   Sum += (Buffer[i] - '0'); }```
If you enter it as an integer, use modulus operations. n % 10 gives the last digit in the number:
Code:

```int Number; int Sum = 0; while(Number != 0) {   Sum += (Number % 10);   Number /= 10; }```
• 05-25-2003
Tonyukuk
I see thanks a lot
• 05-26-2003
Hankyaku
Hi!

If you like to play with strings, try this. (C++)

Code:

```AnsiString NumberString; int MyNumber; /*The entered number*/ int Sum=0; v/*The sum of digits*/ MyNumber=EnterNumber(); NumberString=StrToInt(MyNumber); int length=NumberString.Length()+1; for(int i=1;i<temp;i++)   {   Sum +=NumberString[i];   }```
I like to play with strings. :D

ByZ
Han
• 05-26-2003
grib
Hankyaku, I think you wanted IntToStr and to use length rather then temp in your loop. In any event, none of these are standard. The cool standard way of doing this would be
Code:

```#include<iostream> #include<string> #include<algorithm> using std::cout; using std::endl; struct adder {     unsigned sum;     adder() : sum(0) {}     void operator()(char ch) {sum += ch-'0';}     operator unsigned() {return sum;} }; int main() {     std::string s;     if(std::cin >> s) cout << std::for_each(s.begin(),s.end(),adder()) << endl;     else std::cerr << "Bad Input" << endl;     return 0; }```
Here's a slightly more readable version that also inlcudes a fun premature optomization. Trading n subtractions for a single additonal multiplication and subtraction.
Code:

```using std::cout; using std::endl; int main() {   std::string str;   std::cin >> str;   const unsigned size = str.size();   int sum = 0;   for(unsigned i=0; i != size; ++i) sum += str[i];   sum -= size * '0';   cout << sum << endl;   return 0; }```
• 05-26-2003
7stud
"thanks a lot. but what is '0' NULL or something?"

A variable of type char is really an int type, and every character you read into a char type is stored as a special integer code. The integer codes are listed in an ASCII table, which can usually be found at the back of your programming text.

The integer code for '0' is 48, and the integer code for '8' is 56. So, if you subtract the char '0' from the char '8' and assign it to an int variable, rather than a char variable, you will get 56-48 = 8. If you assign the difference to a char variable, then 8 will be interpreted as an integer code--and the code 8 corresponds to the backspace key.

If you do something like this:

int A= 56;
char B = 56;

and then display A, you will see 56. However, when you try to display B, 56 is interpreted as an integer code and it's first converted to the char it represents, which is 8, and then 8 is displayed.