Sum of Digits of a Number C++ Program
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:
https://www.testingdocs.com/download-and-install-codeblocks/
For more information on Code Blocks IDE, visit the official website of Code blocks IDE:
—
C++ Tutorials
C++ Tutorials on this website:
https://www.testingdocs.com/c-coding-tutorials/
For more information on the current ISO C++ standard
