C++ Program to find the sum of digits of a number
In this program, You will learn how to find sum of digits of a number in C++.
123 => 1 + 2 + 3 = 6
Example: How to find the sum of digits of a number in C++.
#include<iostream>
using namespace std;
int main() {
int a, sum = 0, r;
cout << "Enter a number:";
cin>>a;
while (a > 0) {
r = a % 10;
sum = sum + r;
a = a / 10;
}
cout << "Sum of all digits:" << sum;
return 0;
}
Output:
Enter a number:1234
Sum of all digits:10