C Exercises: Find the integral quotient and remainder of a division
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 Programming Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C program to sort the elements of an array.
Next: Write a C program to return the absolute value of a long integer.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics