w3resource

C Exercises: Find the integral quotient and remainder of a division


7. Quotient and Remainder Calculation

Write a C program to calculate the integral quotient and remainder of a division.

Sample Solution:

C Code:

#include<stdio.h>      // Include the standard input/output header file.
#include<stdlib.h>     // Include the standard library header file.

int main ()          // Start of the main function.
{
long int n, d;    // Declare two long integer variables 'n' and 'd'.
ldiv_t result;    // Declare a variable of type ldiv_t to store the result of the division.

printf("\n Input numerator : ");   // Prompt the user to input the numerator.
scanf("%ld",&n);   // Read the user's input and store it in 'n'.

printf(" Input denominator : ");   // Prompt the user to input the denominator.
scanf("%ld",&d);   // Read the user's input and store it in 'd'.

result = ldiv (n,d);   // Perform the division using the ldiv function and store the result in 'result'.

printf (" quotient =  %ld, remainder = %ld.\n\n",result.quot,result.rem);   // Print the quotient and remainder.

return 0;   // Return 0 to indicate successful execution of the program.
}   // End of the main function.

Sample Output:

 Input numerator : 2500                                                                                       
 Input denominator : 235                                                                                      
 quotient =  10, remainder = 150.

Flowchart:

C Exercises Flowchart: Find the integral quotient and remainder of a division

For more Practice: Solve these Related Problems:

  • Write a C program to compute the quotient and remainder for two large integers using the division operator.
  • Write a C program that calculates the quotient and remainder and verifies the result by reconstructing the original number.
  • Write a C program to compute the quotient and remainder using bitwise operations when dividing by a power of 2.
  • Write a C program to compute the quotient and remainder without using the modulus operator by repeated subtraction.

Go to:


PREV : Array Sorting.
NEXT : Absolute Value of a Long Integer.

C Programming Code Editor:



Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.