C++ Program to Add Digits of Number using While Loop
Write C++ Program to Add Digits of Number using While Loop
// CPP Program to Add Digits of Number using While Loop #include <iostream> using namespace std; int main() { long int n, m; int sum = 0; cout << "Enter the number :--> "; cin >> n; m = n; while(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