66: Write a program in C++ to find the sum of digits of a given number
// Write a program in C++ to find the sum of digits of a given number. Go to the editor
// Sample Output:
// Input a number: 1234
// The sum of digits of 1234 is: 10
// **************************************************************************************************
#include <iostream>
using namespace std;
int main()
{
int num, numr;
cout << "Input a number: ";
cin >> num;
numr = num;
int a = 0, sum = 0;
while (num > 0)
{
a = num % 10;
num = num / 10;
sum = sum + a;
}
cout << "The sum of digits of " << numr << " is: " << sum;
return 0;
}
Comments
Post a Comment