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;

}

## Comments

## Leave a comment