63 043
Assignments Done
99,1%
Successfully Done
In July 2018

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
Expert's answer
#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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions