Home >>Cpp Programs >Sum Of Digits Program in C++

# Sum Of Digits Program in C++

### 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.

### Sum of digits algorithm

In order to retrieve the sum of two digits number you have to follow the below depicted algorithm:

• Receive the number that is entered by the user.
• Second step involves getting the modulus/remainder of the received number.
• Then the sum of the remainder of the number is done.
• In the next step, the number is divided by 10.
• Now, the step 2 will be repeated till the provided number is greater than 0.

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;
}
```
Output :
Enter a number: 12345
Sum is of given number = 15
Total Digit of given number = 5