Sunday, March 8, 2015

Sum of its digits

Script

#include <iostream>
using namespace std;

main()
{
        int number, remainder, sum;

        cout << "Enter an integer: ";
        cin >> number;

        sum = 0;

        while (number > 0)
        {
                remainder = number % 10;
                sum = sum + remainder;
                number = number/10;
        }

        cout << "The sum of the digits is:  " << sum;
        cout << "\n\n";

        return 0;
}

Execution

Enter an integer: 123456789
The sum of the digits is:  45


Enter an integer: 76381634
The sum of the digits is:  38


Enter an integer: 65432
The sum of the digits is:  20

No comments:

Post a Comment