Answer to Question #343669 in C++ for feeona

Question #343669

2.Construct a program in C++ to compute the sum of the digits of the numbers input from the user. Apply all the control structures. Display the number input by the user as well as the sum. Use for loop control structure.


Example display in Console Application

Input number: 93

———output——-

You input number 9 and 3.

The sum of the digits 9 and 3 is 12.


1
Expert's answer
2022-05-22T18:10:31-0400
#include <iostream>
using namespace std;


int main()
{
    int number, sum = 0;
    cout<<"Input number: ";
    cin>>number;
    cout<<"———output——-"<<endl;
    cout<<"You input number ";
    while(number>0){
        sum+=number%10;
        cout<<number%10<<" ";
        number/=10;
    }
    cout<<"the sum is: "<<sum<<endl;
    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
New on Blog
APPROVED BY CLIENTS