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.
#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;
}
Comments
Leave a comment