Back to C Programming
2026-01-245 min read

Modulo operator (C Programming)

Learn Modulo operator (C Programming) step by step with clear examples and exercises.

Why This Matters

The Modulo operator is an essential tool in C programming that helps solve real-world problems involving remainders, divisibility, and cyclic operations. It plays a crucial role in various applications such as date calculations, animation loops, encryption algorithms, and more. Understanding the modulo operator can significantly improve your problem-solving abilities and help you excel in coding challenges.

Prerequisites

Before diving into the Modulo operator, it's essential to have a good understanding of basic arithmetic operators, data types, variables, constants, and expressions in C programming. A solid foundation in C programming is crucial for effectively using the modulo operator.

Core Concept

The Modulo operator (%) calculates the remainder when one number is divided by another. It's a binary operator that follows these rules:

  1. The left operand is the dividend.
  2. The right operand is the divisor.
  3. The result is the remainder of the division operation.

Here are some examples to illustrate how the Modulo operator works:

  • 7 % 3 = 1 (The remainder when 7 is divided by 3)
  • 10 % 2 = 0 (Even numbers have a 0 remainder when divided by 2)
  • -5 % 3 = 2 (Negative numbers also follow the same rules)

Modulo and Positive Numbers

When both operands are positive, the modulo operator behaves as expected. However, Note that that the result of the modulo operation might not always be positive. For example: 5 % 2 = 1, but -5 % 2 = 1 as well.

Modulo and Negative Numbers

When one or both operands are negative, the sign of the result depends on the divisor's sign. If the divisor is positive, the result will have the same sign as the dividend; if the divisor is negative, the result will have the opposite sign. For example:

  • 5 % -3 = -2 (The remainder when 5 is divided by -3)
  • -5 % 3 = 2 (The remainder when -5 is divided by 3)

Worked Example

Let's write a simple C program that calculates the number of days in a month using modulo operator:

#include <stdio.h>

int main() {
int month;

printf("Enter a month (1-12): ");
scanf("%d", &month);

switch(month) {
case 2:
printf("28 or 29 days\n");
break;
case 4:
case 6:
case 9:
case 11:
printf("30 days\n");
break;
default:
printf("%d days\n", (month % 2 == 0) ? 31 : 30);
}

return 0;
}

In this example, we use the modulo operator to determine whether a month has 30 or 31 days based on its number. If the month is even, we assume it has 30 days; otherwise, we assume it has 31 days. This simple program demonstrates how the modulo operator can be used in practical scenarios.

Common Mistakes

Neglecting Signs

When working with negative numbers, it's important to remember that the sign of the result depends on the divisor's sign. Many beginners make the mistake of assuming that the result will always have the same sign as the dividend.

int a = -5;
int b = 3;
printf("%d\n", a % b); // Output: -2, not 2 (because divisor is positive)

Using Modulo on Incorrect Data Types

The modulo operator can only be applied to integral data types like int, char, and enumerated types. Attempting to use it with floating-point numbers or pointers will result in a compile-time error.

float a = 5.2;
printf("%f\n", a % 3); // Compile Error: invalid operands of types 'double' and 'int' to binary '%' operator

Incorrect Use of Modulo in Loops

When using the modulo operator in loops, it's essential to understand that the loop counter starts from zero. This means that if you want to create an animation loop that runs every n frames, you should use i % n != 0 instead of i % n == n-1. The latter will only work correctly when n is a power of two.

for (int i = 0; i < 100; i++) {
if (i % 20 == 19) { // Incorrect, only works for multiples of 20 that are not 0
// Animation frame update code here
}
}

Practice Questions

  1. Write a program that checks whether a number is even or odd using modulo operator.
  2. Given an array of integers, write a function that finds the largest multiple of 5 in the array.
  3. Implement a function that calculates the greatest common divisor (GCD) of two numbers using Binary GCD Algorithm.
  4. Write a program that determines whether a given year is a leap year using modulo operator.
  5. Create a function that generates all unique combinations of a given set of integers, where the order does not matter, using modulo operator to handle cyclic dependencies.
  6. Write a program that finds the smallest number that can be added to a given number to make it divisible by 7.
  7. Implement a function that checks if a given number is prime using modulo operator.
  8. Write a program that generates all Fibonacci numbers up to a given limit using modulo operator to optimize memory usage.
  9. Create a function that determines the day of the week for a given date using modulo operator and a predefined lookup table.
  10. Implement a function that finds the number of unique subarrays in a given array with a specific sum using modulo operator to handle cyclic dependencies.

FAQ

What happens when we divide a number by zero using modulo operator?

Attempting to perform division by zero results in undefined behavior, so it's not recommended to use the modulo operator with zero as the divisor. However, some programming languages (like Python) handle this case gracefully and return an error instead of undefined behavior.

Can we use modulo operator on floating-point numbers?

No, the modulo operator can only be applied to integral data types like int, char, and enumerated types in C programming. Using it with floating-point numbers will result in a compile-time error. If you need to find remainders of floating-point numbers, consider using libraries that provide this functionality or rounding the floating-point numbers to integers before performing the modulo operation.

Why can't we use the modulo operator with floating-point numbers directly?

The modulo operator is a binary operator that calculates remainders of integer division. Floating-point numbers are real numbers, and they don't have discrete remainders in the same way as integers do. Therefore, it would be challenging to define a meaningful result for the modulo operation on floating-point numbers without rounding or truncation, which can lead to unintuitive results.

Why does the loop counter start from zero when using modulo operator in loops?

The loop counter starts from zero because C uses zero-based indexing. This means that when you access an array element with an index, the first element is at index 0. When using modulo operator in loops, it's essential to understand this behavior and adjust your logic accordingly if necessary.