Sum of Digits Example C++ Program
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
Top Pages
- Simple Example Program For Constructor In C++
- Simple Example Program For Parameterized Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Binary Operator Overloading Using C++ Programming
- Simple Merge Sort Program in C++
- Factorial Using Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Unary Operator Overloading Using C++ Programming