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

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    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>
    Last edited by Tonyukuk; 05-25-2003 at 10:08 AM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148
    Try this
    Code:
    	char *str = "189";
    	int sum = 0;
    	for(int i = 0;str[i];i++)
    		sum += str[i] - '0';
    	cout << sum << endl;

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    thanks a lot. but what is '0' NULL or something?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    I see thanks a lot

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    105
    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.

    ByZ
    Han

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    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;
    }

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "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.
    Last edited by 7stud; 05-26-2003 at 05:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. GPA Program Problems
    By Clint in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 10:45 AM