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

Write a program in C that accepts two integer inputs from a user (through scanf(

ID: 3824289 • Letter: W

Question

Write a program in C that accepts two integer inputs from a user (through scanf() ), and assigns them to integer variables a and b, where a is the numerator and b is the denominator. The program declares two more int variables, c and d, where c is assigned the result of a/b and d is assigned the result of a%b. Print out the values of the integers entered, the result of their division and the result of their mod division within appropriately descriptive text (e.g., “The result of an integer division of <a> divided by <b> is <c> etc. etc....”).

Explanation / Answer

Here is your program. I have added comments to help you understand the logic;-

#include<stdio.h>

int main() {
   int a,b,c,d;//declaration of four integers
   printf("Enter two numbers ");
   scanf("%d",&a); //assigning first input to a
   scanf("%d",&b); //assigning second input to b
  
   while(b == 0) { //Denominator cannot be zero so adding the check. It asks for input till user enters number other than 0
       printf("Denominator cannot be zero. Please enter second number again. ");
       scanf("%d",&b);
   }
   c = a/b;// division
   d = a%b;// mod division
  
   printf("The result of an integer division of %d divided by %d is %d ",a,b,c);
   printf("The result of an integer mod division of %d divided by %d is %d ",a,b,d);
   return 0;
}

Sample run: -

Enter two numbers
12
0
Denominator cannot be zero. Please enter second number again.
5
The result of an integer division of 12 divided by 5 is 2
The result of an integer mod division of 12 divided by 5 is 2

--------------------------------
Process exited after 16.5 seconds with return value 0
Press any key to continue . . .

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