+ 1
How to write a program to display the sum of the digits of a number entered by the user??
For ex , if the user entered 325, the result is 3+2+5=10
3 odpowiedzi
+ 2
int num;
int sum=0;
cout << "enter any number" << endl;
cin >> num;
while (num!=0)
{
num=num%10;
sum= sum + num;
num=num/10;
}
cout << sum;
return 0;
+ 1
try to write it your self its a simple use of for loop or while what ever you want.
0
#include <iostream>
using namespace std;
int main ()
{
int no,sum=0;
cout <<"enter a number"<<endl;
cin >>no;
for (;no!=0;no=no/10 )
{
no=no%10;
sum=sum+no;
}
cout <<"sum of number is "<<sum;
return 0;
}