Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to Compute Quotient and Remainder?
Given two numbers dividend and divisor, we need to write a C program to find the quotient and remainder when the dividend is divided by the divisor.
In division, we see the relationship between the dividend, divisor, quotient, and remainder. The number which we divide is called the dividend. The number by which we divide is called the divisor. The result obtained is called the quotient. The number left over is called the remainder.
55 ÷ 9 = 6 and 1 Dividend Divisor Quotient Remainder
Syntax
quotient = dividend / divisor; remainder = dividend % divisor;
Example 1: Computing Quotient and Remainder
The following example demonstrates how to compute quotient and remainder using division (/) and modulo (%) operators −
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
dividend = 55;
divisor = 9;
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Dividend = %d
", dividend);
printf("Divisor = %d
", divisor);
printf("Quotient = %d
", quotient);
printf("Remainder = %d
", remainder);
return 0;
}
Dividend = 55 Divisor = 9 Quotient = 6 Remainder = 1
Example 2: User Input Division
This example takes dividend and divisor values from the user −
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Check for division by zero
if (divisor == 0) {
printf("Error: Division by zero is not allowed.
");
return 1;
}
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient = %d
", quotient);
printf("Remainder = %d
", remainder);
return 0;
}
Enter dividend: 17 Enter divisor: 5 Quotient = 3 Remainder = 2
Key Points
- The / operator performs integer division and returns the quotient.
- The % (modulo) operator returns the remainder after division.
- Always check for division by zero to avoid runtime errors.
- For integer division, any fractional part is discarded.
Conclusion
Computing quotient and remainder in C is straightforward using the division (/) and modulo (%) operators. Always validate the divisor to prevent division by zero errors.
