If you sum the digits of a number and the result is a multiple of 3, then the or
ID: 3751769 • Letter: I
Question
If you sum the digits of a number and the result is a multiple of 3, then the original number is a multiple of 3. For large sums, you may have to think a bit before deciding whether a sum is a multiple of 3. To make life easier, you can sum the digits in the sum and see whether it results in a multiple of 3. You can continue to do this until it becomes obvious that the number is a multiple of 3-or not. Write a program that will prompt the user for an integer up to 5 digits long, sum the digits in this number, and display the results. It should then sum the digits in the sum and display the results. Your program should consist of a main() function and the sum-digits() function. (Hint: Recall how you used divmod ) to find the number of dollars, quarters, dimes, nickels, and pennies to make change in your last lab.) The output should be similar to the following Enter an integer up to 5 digits long: 99999 2 Sum of digits in 99999: 45 Sum of digits in 45: 9 Enter an integer up to 5 digits long: 1234 2 Sum of digits in 1234: 10 Sum of digits in 10: 1 Enter an integer up to 5 digits long: 75 2 Sum of digits in 75: 12 Sum of digits in 12: 3Explanation / Answer
Since no particular programing language is mentioned, I have written code in C++
#include<iostream>
using namespace std;
int sum_digits(int num)
{
int sum=0;
while(num)
{
sum=sum+(num%10);
num=num/10;
}
return sum;
}
int main()
{
int n,m;
cout<<"Enter an integer up to 5 digit long: ";
cin>>n;
while(n)
{
m=sum_digits(n);
cout<<"Sum of digits in "<<n<<": "<<m<<endl;
n=m;
if(n/10==0)
break;
}
}