Question #344308

Construct a c++ program to compute the sum of the digits of the numbers input from the user. Display the number input by the user as well as the sum. Use for loop Control structure.

Expert's answer

#include<iostream>
using namespace std;

int main(){
    int n;
    int sum = 0;    
    cout<<"Enter int N:  ";
    cin>>n;
    cout<<"The sum of the digits of the number "<<n<<":  ";

    while(n != 0){
        sum = sum+ n % 10;
        n = n /10;
    }
    cout<<sum<<endl;
}

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!

LATEST TUTORIALS
APPROVED BY CLIENTS