Find sum of digits of a number
Here you will get the source code of C++ program to find sum of digits of a number.
Sum of digits is the sum of all digits of a positive number.
Program to find sum of digits of a number in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<iostream> using namespace std; int main() { long int rem,num,num1,sum=0; cout<<"Enter a number : "; cin>>num; num1=num; while(num ! = 0) { rem = num % 10; sum = sum + rem; num = num / 10; } cout<<endl<<"Sum of digits of "<<num1<< " is "<<sum; return 0; } |
Output
Enter a number : 1234
Sum of digits of 1234 is 10
Check out our other C++ programming Examples