In: Computer Science
C++
Plan and code a program utilizing one or more repetition structures to solve the following problem:
Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. A number is a multiple of 9 if the sum of its digits is evenly divisible by 9. Determine if a number is a multiple of 9 by adding together the individual digits of the number and determining if the sum of the digits is evenly divisible by 9. Input should be limited to numbers from 1 through 1000.
Check your solution with valid and invalid data. Use good program design, efficient code, and document your code with explanatory comments throughout.
Be sure to include adequate error handling in your program and error data when you run the program to demonstrate error handling.
Input
One or more numbers entered by a user.
Output
The input number, the sum of its digits. and whether the number is a multiple of 9 or not.
ANSWER - C++ code pasted below.
#include <iostream>
using namespace std;
void sumofdigits(int x)
{
//initialize sum to 0 and take a copy of a to a1
int sum=0;
int a1=x,digit;
//continously divide x by 10 to get the remainder until x reaches
0
while(x!=0){
//taking the digit
digit=x%10;
//adding the sum with digit
sum=sum+digit;
//dividing the number with 10 to remove the last digit
x=x/10;
}
if(sum%9==0)
cout<<a1<<" is a multiple of 9"<<endl;
else
cout<<a1<<" is not a multiple of 9"<<endl;
}
//main progarm
int main()
{ int a, b, c;
// Reading three numbers a, b, c
cout<<"Enter three numbers between 1 and 1000:";
cin>>a>>b>>c;
//checking whether the nunbers are in the range 1--1000
if(a<1 || a>1000){
cout<<"Number "<<a<<" is out of range!..Enter a
number between 1 and 1000";
return 0;
}
if(b<1 || b>1000){
cout<<"Number "<<b<<" is out of range!..Enter a
number between 1 and 1000";
return 0;
}
if(c<1 || c>1000){
cout<<"Number "<<c<<" is out of range!..Enter a
number between 1 and 1000";
return 0;
}
//function calling for a
sumofdigits(a);
//function calling for b
sumofdigits(b);
//function calling for c
sumofdigits(c);
return 0;
}
Output Screen- Test case 1
Output Screen - Test case 2