Question

In: Computer Science

C language and it has to be a while loop or a for loop. Use simple...

C language and it has to be a while loop or a for loop.

Use simple short comments to walk through your code.
Use indentations to make your code visibly clear and easy to follow.
Make the output display of your program visually appealing.
There is 10 points deduction for not following proper submission structure.

An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that:

Would read an input from the keyboard. The input should be a whole positive number.
Then display each digit, starting with the rightmost digit.
At last decide if the number if divisible by 9 or not.

  1. Calculate and display the sum of the digits.

  2. Display whether the input number is divisible by 9 or not. (

Hint: You would need to write a loop that extracts each digit in the number. Use the % operator to get each digit; then use / to remove that digit.

As an example: 154368 % 10 gives 8 and 154368 / 10 gives 15436. The next digit extracted should be 6, then 3 and so on.

Test your program on the following numbers:

n = 154368

n = 621594

n = 123456

Solutions

Expert Solution

Code:

#include<stdio.h>
int main(){
int n, digit, sum = 0;//variable declaration and initialization
printf("Enter a number: ");//prompt the user to enter a number
scanf("%d", &n);//take input and store in variable n
printf("Digits are:");//a dialog saying digits are:
//The following while loop executes when n>0
while(n > 0){
digit = n%10;// n%10 is done to extract the last digit of n and is stored in digit variable
n = n/10;// n/10 is dont to remove the last digit from n and store the remaining value in n
printf("%d\t", digit);//prints the digit
sum += digit;// adds the digit to the sum and store the total in sum variable.
}
//checkis if sum of digits is divisible by 9
if(sum % 9 == 0){
printf("\nThe given number is divisible by 9.\n");//if sum is divisible by 9, this line is executed
}
//if the sum of digits is not divisible by9, the following is executed.
else{
printf("\nThe given number is not divisible by 9.\n");
}
}

Code in the image:

Output:


Related Solutions

All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
C# language. Answer in a discussion format. What is a loop? When do you use a...
C# language. Answer in a discussion format. What is a loop? When do you use a loop versus a selection statement? What are some examples when an infinite loop is appropriate? Describe one of the following three types of loops supported in C#: while loop for loop do loop (or do-while loop) Describe the steps necessary to make a while loop end correctly: Explain the difference between incrementing and decrementing loop control variables. Explain the benefits of using both pretest...
write an operational semantic for a while loop in c++
write an operational semantic for a while loop in c++
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the for loop, and selection. Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. If the user enters a 0 or a negative number the program should exit with a message to the user indicating they chose to exit. If...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE. Problem: Driving Function driving(): Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
C++ program Reference: while loop, if....else, for loop, the % operator MARTIAN BOOLEAN MULTIPLICATION OBJECTIVE: To...
C++ program Reference: while loop, if....else, for loop, the % operator MARTIAN BOOLEAN MULTIPLICATION OBJECTIVE: To compute the product of two integers using the Martian method of multiplication. INPUT: 5 pairs of positive integers to be multiplied. To be read in one pair at a time. USE A FOR LOOP TO CONTROL THE READING OF THE INPUT OUTPUT: Submit the following: The psuedocode or notes on how you will attack the problem Hand in the complete C++ program, properly commented...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT