Q:

Write a program in C++ to compute the sum of the digits of an integer

0

Write a program in C++ to compute the sum of the digits of an integer

Sample Output:

 Compute the sum of the digits of an integer:                          
-------------------------------------------------                      
 Input any number: 25                                                  
 The sum of the digits of the number 25 is: 7

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
using namespace std;
 
int main()
{
	int num1,sum,n;
	sum=0;
    cout << "\n\n Compute the sum of the digits of an integer:\n";
	cout << "-------------------------------------------------\n";
	cout << " Input any number: ";
	cin>> num1;
	n=num1;
        while (num1 != 0) 
		{
            sum += num1 % 10;
            num1 /= 10;
        }
            cout<<" The sum of the digits of the number "<<n<<" is: " << sum <<endl;  
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now