+ 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

12th Aug 2016, 6:48 PM
Ajax 7/11
Ajax 7/11 - avatar
3 Respostas
+ 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;
12th Aug 2016, 7:21 PM
Tarun kumar parashar
Tarun kumar parashar - avatar
+ 1
try to write it your self its a simple use of for loop or while what ever you want.
12th Aug 2016, 7:17 PM
Tarun kumar parashar
Tarun kumar parashar - avatar
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; }
13th Aug 2016, 3:32 PM
Tushar Chetan
Tushar Chetan - avatar