The modulus operator, often denoted as % in many programming languages including C, provides the remainder of a division operation. Despite its seemingly straightforward concept, the modulus operator is an incredibly powerful tool when applied creatively and with understanding.

In this article, we'll delve into the various facets of the modulus operator in C: how it works, its uses, its behavior with different operand types, and common scenarios where it comes in handy.

Modulus Operator in C

In C programming, the modulus operator (%) returns the remainder of dividing the first operand by the second operand. It's commonly used in arithmetic operations, especially for tasks like checking if a number is even or odd, calculating remainders, or cycling through values in a circular buffer. For instance, 5 % 2 returns 1, as 5 divided by 2 leaves a remainder of 1. Similarly, -5 % 2 returns -1, as -5 divided by 2 leaves a remainder of -1. Note that the result of the modulus operator is always in the range of the second operand's sign.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Syntax of Modulus Operator in C

In C programming, the modulus operator is represented by the percent sign (%). It is used to find the remainder when one integer is divided by another. The syntax of the modulus operator is as follows:

result = dividend % divisor;

Here, dividend is the number being divided, divisor is the number by which the dividend is divided, and % is the modulus operator.

Return Values for Modulus Operator in C

The return values for the modulus operator are the remainder after the division. The result will be an integer representing the remaining value after dividing the dividend by the divisor. If the result is 0, it means that the dividend is evenly divisible by the divisor. For example,

C

C

#include <stdio.h>

int main() { int dividend = 10; int divisor = 3; int result;

result = dividend % divisor;

printf("The remainder when %d is divided by %d is %d\n", dividend, divisor, result);

return 0; }

Output

Working Of Modulus Operator In C

The modulus operator in C is used to find the remainder of a division operation. If you're familiar with division from arithmetic, you know that it usually involves a dividend, a divisor, and potentially a remainder. Here's how it works in a simple C code snippet:

C

C

#include<stdio.h>

int main() { int dividend = 10; int divisor = 3; int remainder = dividend % divisor;

printf("The remainder of %d divided by %d is %d\n", dividend, divisor, remainder); return 0; }

In this code, the remainder of 10 divided by 3 is 1, which is what the modulus operator gives us.

Calculation of Modulus Operator in C

The modulus operator (%) in C calculates the remainder of the division operation between two operands. It follows the formula:

𝑎mod 𝑏=𝑎−(𝑎/𝑏)×𝑏

Where:

𝑎 is the dividend or the number being divided.

𝑏 is the divisor or the number by which 𝑎a is being divided.

This operation returns the remainder after dividing 𝑎 by 𝑏. If 𝑎a is divisible by 𝑏, the result is 0. Otherwise, the result is the remainder.

Applications of the Modulus Operator

Now that we've seen how the modulus operator works, let's explore some of its practical applications.

1. Checking for Even or Odd Numbers

The modulus operator can easily check if a number is even or odd. If a number modulo 2 equals 0, the number is even. If it equals 1, the number is odd. Here is a code snippet illustrating this:

C

C

#include<stdio.h>

int main() { int num = 10; if(num % 2 == 0) { printf("%d is an even number.\n", num); } else { printf("%d is an odd number.\n", num); } return 0; }

Output

2. Implementing a Digital Clock

The modulus operator can be used to wrap values, such as implementing a digital clock that goes from 23:59 to 00:00. Here, modulus 24 and modulus 60 are used for hours and minutes respectively.

int main() { int dividend = 10; int divisor = 3; int remainder = dividend % divisor;

printf("The remainder of %d divided by %d is %d\n", dividend, divisor, remainder);

return 0; }

Explanation:

In this example, we declare two integers dividend and divisor with values 10 and 3 respectively.

We then use the modulus operator % to calculate the remainder of dividend divided by divisor.

Finally, we print the result, which will be 1, as 10 divided by 3 leaves a remainder of 1.

Some Exceptional Restrictions of the Modulo Operator in C

Division by Zero: Attempting to divide by zero using the modulus operator results in undefined behavior. It's crucial to ensure that the divisor is never zero to avoid unexpected results or program crashes.

Floating-point Operands: The modulus operator cannot be used with floating-point operands (e.g., float or double). It only works with integer operands.

Signedness: The result of the modulus operator follows the sign of the dividend in C. For example, -10 % 3 yields -1, not 2, as some might expect. This behavior can lead to unexpected results if not considered carefully.

The Modulus Operator with Different Operand Types

The modulus operator in C only works with integer operands. If you try to use it with a float or a double, the compiler will throw an error.

What is the use of the modulus (%) operator in C++?

The modulus (%) operator in C++ returns the remainder of dividing the first operand by the second. It's commonly used to check divisibility, calculate remainders, or cycle through values.

What is 1 modulo 10 in C?

In C, 1 modulo 10 (1 % 10) equals 1. The modulo operator (%) returns the remainder of dividing the first operand (1) by the second operand (10), which is 1.

What is an example of a modulus?

An example of a modulus operation is 7 % 3, which equals 1. This operation calculates the remainder when 7 is divided by 3, resulting in 1.

What is the symbol of modulus?

The symbol of modulus is %. In mathematical notation, the modulus operator is represented by the percent sign (%), which is placed between the dividend and the divisor.

What is the difference between division and modulus operator in C?

In C, the division operator (/) calculates the quotient of dividing the first operand by the second, while the modulus operator (%) calculates the remainder. Division returns the whole number result, while modulus returns the remainder.

Conclusion

The modulus operator in C is a simple yet powerful tool with various applications. While it only works with integers, its uses range from simple arithmetic to more complex tasks such as checking even-odd numbers or implementing a digital clock. So, as you continue your journey with C, remember to leverage this versatile operator when it suits your needs.