In this article, you will learn a C++ program to find the sum of digits by using a while loop and mathematical operators. For example, if the user enters 345 the sum of the digits will be 12.

## Example 1: Sum of digits in C++

This program asks the user to enter any number through the console screen. The while loop goes through each and every digit of the given number and adds them. After computation, the program displays the sum of digits on the screen.

```
// C++ Program to find Sum of digits
#include <iostream>
using namespace std;
int main()
{
int num,sum=0,a;
cout<<"Enter any number: ";
cin>>num;
while(num>0)
{
a=num%10;
sum=sum+a;
num=num/10;
}
cout<<"Sum of digits is: "<<sum<<endl;
return 0;
}
```

### Output

```
Enter any number: 345
Sum of digits is: 12
```