C++ friend told has told you an interesting fact: “Take 5 digit number and sum i
ID: 3910217 • Letter: C
Question
C++
friend told has told you an interesting fact: “Take 5 digit number and sum it’s digits. If the sum is divisible by 9, then the number is divisible by 9”. You are going to write a program to test this out.
Your program is to:
* Ask the user for a positive 5 digit value
* If the value is NOT 5 digits, reprompt UNTIL a valid number is provided
* Compute the sum of the 5 digits (Hint: You will need to loop once to get each
digit recall that a%10 provides the leftmost digit of a number, and a/10 removes
the leftmost digit from a number)
* Print out the computed sum.
* If the sum is evenly divisible by 9 and the original number was divisible by 9,
a message “It Worked”. Otherwise post a message indicating “Doesn’t Work”.
Once you are sure that your program works correctly for one input number, update your program to allow the user to continue to test numbers until they indicate that they want to exit the program. Program is clearly commented and indented to indicate the flow of control and what is going on.
Explanation / Answer
PROGRAM
#include<iostream>
using namespace std;
// implelement sumOfDigits() function and return integer value
int sumOfDigits(int x)
{
int sum=0,r; // declare two integer variables sum,r and initialize sum=0
while(x!=0) // create while loop until not equal to 0
{
r=x%10; // find remainder of x value
sum+=r; // calculate sum of remainder values sum=sum+r
x/=10; // finding coefficient value of x
}
return sum; // return sum
}
// implement count5Digits() function and return integer value
int count5Digits(int x)
{
int count=0; // declare and initialize integer value count=0
while(x!=0) // create while loop until not equal to 0
{
int r=x%10; // find remainder of x value
x/=10; // finding coefficient value of x
count++; // counting number of digits in x
}
return count; // return count
}
int main()
{
while(1) // create infinite while loop until type num=0
{
int num,n,sum; // declare integer variables num,n,sum
int flag=0; // declare integer variable flag with initialize 0
while(1) // create infinite while loop break
{
cout<<" (Enter 0 to Exit) Enter 5 digit number: ";
cin>>num; // reading input number
n=num; // assign original number to n
if(num==0) exit(0); // check condition num=0 then exit from this program
int c=count5Digits(num); // calling count5Digits() function and receive count digits in number(num)
sum=sumOfDigits(num); // calling sumOfDigits() function and receive sum of digits in number(num)
if(c>=5) // check condition count digits is greater than or equal 5
{
// then, check condition whether sum and num is divisible 9
// then, assign flag=1 and break
if(sum%9==0&&n%9==0) flag=1;
break;
}
}
if(flag==1) // check condition is true
cout<<" It Worked."<<endl; // then, display this message
else
cout<<" Doesn't Work."<<endl; // else, display this message
}
return 0;
}
OUTPUT
(Enter 0 to Exit)
Enter 5 digit number: 1
(Enter 0 to Exit)
Enter 5 digit number: 12
(Enter 0 to Exit)
Enter 5 digit number: 123
(Enter 0 to Exit)
Enter 5 digit number: 1234
(Enter 0 to Exit)
Enter 5 digit number: 12345
Doesn't Work.
(Enter 0 to Exit)
Enter 5 digit number: 32654
Doesn't Work.
(Enter 0 to Exit)
Enter 5 digit number: 36369
It Worked.
(Enter 0 to Exit)
Enter 5 digit number: 34569
It Worked.
(Enter 0 to Exit)
Enter 5 digit number: 23456
Doesn't Work.
(Enter 0 to Exit)
Enter 5 digit number: 99999
It Worked.
(Enter 0 to Exit)
Enter 5 digit number: 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.