In C programming, the modulo operator (%
) is a handy tool for determining the remainder when dividing two numbers. This is useful when you want to check if a number is divisible by another or when you need to find the remainder of a division operation. In this blog post, we will explain how the modulo operator works and provide a simple C program to demonstrate its usage.
What is the Modulo Operator?
The modulo operator (%
) in C returns the remainder of the division of one number by another. For example:
10 % 3
returns1
because when you divide 10 by 3, the remainder is 1.9 % 3
returns0
because 9 is evenly divisible by 3 with no remainder.
The modulo operator is often used in programming to check for divisibility and to calculate remainders.
Syntax of Modulo Operator:
result = a % b;
Here, a
is the dividend, and b
is the divisor. The result
will store the remainder of a
divided by b
.
Example: C Program to Check Divisibility and Print Remainder
Let’s write a C program that uses the modulo operator to check whether a number is divisible by another and print the remainder.
#include <stdio.h>
int main() {
int num1, num2, remainder;
// Input two numbers
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// Check if num2 is not zero to avoid division by zero
if (num2 != 0) {
// Calculate remainder using modulo operator
remainder = num1 % num2;
// Check if the number is divisible
if (remainder == 0) {
printf("%d is divisible by %d.\n", num1, num2);
} else {
printf("%d is not divisible by %d. Remainder: %d\n", num1, num2, remainder);
}
} else {
printf("Division by zero is not allowed.\n");
}
return 0;
}
Explanation of the Program:
- Input: The program prompts the user to input two integers:
num1
(the number to be divided) andnum2
(the divisor). - Modulo Operation: The modulo operator (
%
) is used to find the remainder whennum1
is divided bynum2
. - Divisibility Check: If the remainder is
0
, the program outputs thatnum1
is divisible bynum2
. Otherwise, it prints the remainder. - Error Handling: If the user enters
0
as the second number (num2
), the program avoids division by zero and outputs an error message.
Sample Output
If the user enters 10
and 3
as input:
Enter two integers: 10 3
10 is not divisible by 3. Remainder: 1
If the user enters 9
and 3
as input:
Enter two integers: 9 3
9 is divisible by 3.
If the user enters 10
and 0
as input:
Enter two integers: 10 0
Division by zero is not allowed.
When to Use the Modulo Operator
The modulo operator is especially useful in the following scenarios:
- Divisibility Checks: To check whether one number is divisible by another.
- Cycling Through Values: For example, when iterating over a fixed-size array in a circular manner.
- Finding Remainders: When you need to calculate the remainder after division, such as in time calculations.
The modulo operator is a powerful tool in C programming for performing mathematical operations involving division. By using it, you can easily check if a number is divisible by another and calculate the remainder. This is a simple yet fundamental operation that is widely used in programming.
1 thought on “C Program for Modulo Operator: Check Divisibility and Print Remainder”