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