Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code in C An integer is divisible by 9 if the sum of its digits is divisibleExam

ID: 3891018 • Letter: C

Question

Code in C

An integer is divisible by 9 if the sum of its digits is divisibleExample output: by 9. Develop a program which will call UDF: int get input(); to prompt the user for an integer and return this user input to main() Call UDF: Enter an integer: 5463 3 4 5 5463 is divisible by 9 void display int val); to display each digit of the integer starting with the rightmost digit. Your program should also determine whether or not thee (user-entered) integer is divisible by 9. Testing should include using the following numbers. n = 154368 n = 621594 n = 123456 Hint: Use the modulus (%) operator to get each digit, then use / to remove the digit. So 154368 % 10 gives 8 and 154368/10 gives 15436, The next digit extracted should be 6, then 3 and son on. Enter an integer: 154367 7 3 4 5 154367 is not divisible by 9

Explanation / Answer

#include<stdio.h>
int get_input(){
printf("Enter an Integer:");
int n;
//get user input
scanf("%d",&n);
//get a valid number untill between 1-999999
while(n<1 || n>999999){
printf("Enter between(1-999999):");
scanf("%d",&n);
}
return n;
}
void display(int val){
//set sum to zero
int sum = 0;
//set n to val
int n = val;
//while n has digits
while(n>0){
//print digit and add it to sum
printf("%d ",n%10);
sum += n%10;
//reduce digit from back
n/=10;
}
//if sum is evenly divisible by 9.
if(sum%9==0){
printf("%d is divisible by 9 ",val);
}
else{
printf("%d is not divisible by 9 ",val);
}
}
int main(){
int val = get_input();
display(val);
return 0;
}

/* sample output
Enter an Integer: 154368
8
6
3
4
5
1
154368 is divisible by 9

Enter an Integer: 621594
4
9
5
1
2
6
621594 is divisible by 9

Enter an Integer: 123456
6
5
4
3
2
1
123456 is not divisible by 9
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote