# Sum of Digits of a Number C++ Program

Document Contents

### Overview

C++ program to calculate the sum of digits of a number. The program prompts the user to enter the number. For example: Enter number: 7865 The output should be  7+ 8 + 6 + 5 = 26

### C++ Program

```#include
#include
#include
using namespace std;
int main()
{
int sumOfDigits;
int quotient;
int digit;
int number;

cout << "Enter a positive number:= "; cin >> number;
sumOfDigits =0;
if (number>=0)
{
quotient =number;
while (!(quotient==0))
{
digit =quotient % 10;
sumOfDigits =sumOfDigits+digit;
quotient =floor(quotient/10);
}
cout << "Sum of digits of the number " << number << " is =: " << sumOfDigits << endl;
}
else
{
cout << "ERROR : Enter positive number" << endl;
}

return 0;
}

```

### Sample Output

Sample output of the program is shown below: Enter a positive number:= 7865 Sum of digits of the number 7865 is =: 26   The IDE used in the program is Code:: Blocks. To download and install Code Blocks follow the link: