# 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;

}

#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 orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment