Answer to Question #4885 in C++ for Ghiwa

Question #4885
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits
1
Expert's answer
2011-10-27T12:48:00-0400
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
int a, S = 0, divisor = 1;

cout<<"Enter an integer number: ";
cin>>a;
if (a<0) a = -a;
cout<<"Digits:
";
if (a != 0)
{
while (a/divisor > 0)
divisor*=10;
do
{
divisor/=10;
cout<<a/divisor<<" ";
S+=a/divisor;
a%=divisor;
}
while (divisor > 1);
}
else
cout<<0;
cout<<"
Sum of digits is "<<S<<endl;
getch();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS