a simple program that counts and sums digits. How can I make it work?

Posted by user1710386 on Stack Overflow See other posts from Stack Overflow or by user1710386
Published on 2012-09-30T21:30:51Z Indexed on 2012/09/30 21:37 UTC
Read the original article Hit count: 164

Filed under:
|
|

So I've to write a simple program(that loops) where you can enter an int and it spews out the number count and the sum of the numbers. Since I am such a tard when it comes to programming, I just scavanged the code online and tried to piece it together. I guess the sum block screws with n, but I am not really sure. Anyway, I would really appreciate it if somebody could point out mistakes and show me how can I make it work.

#include <iostream>

using namespace std;

int main()
{
while(1)

{
int i,p,n,sum=0;  //sum block
 cout<<"enter an int: ";
 cin>>n;



 {
 while(n!=0)
 {
 p=n % 10;
 sum+=p;
 n=n/10;

}
cout<<"int digit sum: "<<sum <<endl;
  }

  {   int count = 0;
   while(n)
   {
      n /= 10;
      ++count;
   }

   cout <<"number of digits: " << count << '\n';}



}
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about sum