Home >>Cpp Programs >Sum Of Digits Program in C++
The sum of digits program in C++ is generally used to obtain the sum a number's digits that it possesses. For instance; the sum of digits of 30 will be 3 + 0 = 3. Writing this program is very simple in C++ programming language as it requires only the help of mathematical operation and the loop only.
In order to retrieve the sum of two digits number you have to follow the below depicted algorithm:
Here is the program of the sum of the digits that will give you deep understanding of the topic and make you understand that how these things work:
#include <iostream> using namespace std; int main() { int num,sum=0,rem,count; cout<<"Enter a number: "; cin>>num; while(num>0) { rem=num%10; sum=sum+rem; num=num/10; count++; } cout<<"Sum is of given number = "<<sum<<endl; cout<<"Total Digit of given number = "<<count<<endl; return 0; }