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

do number 2 only and please show that code works through command prompt. #1 ques

ID: 3797961 • Letter: D

Question


do number 2 only and please show that code works through command prompt. #1 question is shown so you can see what number 2 is based off of.

1. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program to display each digit, starting with the rightmost digit. Your program should also determine whether or not the number is divisible by 9. Test it on the following numbers: n 154368 n 621594 123456 Hint: Use the operator to get each digit; then use to remove that digit So 154368 10 gives 8 and 154368 10 gives 15436. The next digit extracted should be 6, then 3 and so on 2. Redo programming project l by reading each digit of the number to be tested into a type char variable digit. Display each digit and form the sum of the numeric values of the digits. Hint: The numeric value of digit is (int) digit (int)

Explanation / Answer

C Code:

#include <stdio.h>

int main()
{
    char numStr[10], digit;
    int sum = 0, i, l;
    printf("Enter a number ");
    scanf("%s", numStr);
    l = strlen(numStr);
    for (i = 0; i<l ; i++) {
        digit = numStr[i];
        printf("%c ", digit);
        sum = sum + (int)digit - (int)'0';
    }
  
    printf(" Sum = %d ",+sum);
    return 0;
}

Terminal window:

sh-4.2$ gcc -o main *.c                                                                                                                                                  

sh-4.2$ main                                                                                                                                                             

Enter a number                                                                                                                                                           12459                                                                                                                                                                    1 2 4 5 9                                                                                                                                                               Sum = 21