Sum of Digits Example C++ Program in C++
On this page (5sections)
About this program
This is an example program in c common example program. Read the concept first: C++ Variables and Data Types, then study the code and output below.
Sum of Digits Definition:
Here modulus operator is used to add the digits of the input integer.
Sum of Digits Example Program
/*## Sum of Digits is odd or even In C++*/
/*## Calculation C++ Programs, Datatype C++ Programs, Basic C++ Programs*/
#include <iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int main() {
// Declare Variables
int inputNumber, temp, total = 0, remainder;
cout << "Simple C++ Program : Sum of Digits\n";
cout << "\nEnter a number whose sum is to be found : ";
cin>>inputNumber;
temp = inputNumber;
while (temp != 0) {
remainder = temp % 10;
total = total + remainder;
temp = temp / 10;
}
cout << "\nSum of digits of the number : " << inputNumber << " = " << total;
getch();
return (0);
}
Sample Output:
Simple C++ Program : Sum of Digits
Enter a number whose sum is to be found : 3421
Sum of digits of the number : 3421 = 10
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- C++ Variables and Data Types — Tutorial — data types and variable declaration.
- Factorial Using Loop Example Program In C++ — More in c common example program.
- Factorial Using Function Example Program In C++ — More in c common example program.