C++ Program to Add Digits in Number using For Loop
Write C++ Program to Add Digits in Number using For Loop
// CPP Program to Add Digits in Number using For Loop #include <iostream> using namespace std; int main() { long int n, m; int sum = 0; cout << "Enter the number :--> "; cin >> n; m = n; for(; n > 0 ; ) { int remainder = n % 10; sum = sum + remainder; //sum of digits n = n / 10; } cout << "Sum of the digits of number " << m << " : " << sum; return 0; }
Output:
Enter the number :--> 654321
Sum of the digits of number 654321 : 21