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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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