# What is Modulus Operator in C Programming?

By Anjnee Bhatnagar|Updated : October 11th, 2022

In the C arithmetic operators, the Modulus Operator is an addition that operates on two possible operands. To obtain a result, it divides the provided numerator by the denominator. Put another way, it results in a remainder for the integer division. Thus, the excess amount is also always an integer quantity. In the absence of a remainder, the remainder is equal to zero (0).

Modulus Operator in C PDF

Here, we will discuss the modulus operator in C in detail, along with its syntax, working, calculation, and a few examples to know the concept better for the GATE CSE exam.

## What is Modulus Operator in C?

The symbol % denotes the modulus operator in C. It is an arithmetic operator. Modulus Operator in C programming language is an important part of the GATE CSE syllabus

### Syntax of Modulus Operator in C

Let's assume that the variables p and q in a particular code are both integers. The modulus expression changes to:

p%q

The potentials for return values when p is divided by q, a remnant is left over. For example, the following return values are potential:

• The outcome of the equation is zero if and only if p is entirely divided by q. (0).
• The outcome will be an integer value that is non-zero if q cannot divide p entirely. In other words, the remaining amount will fall between [1, x-1].
• Division by zero causes a compile-time error if p is 0.
• If q is 0 and p is an integer value, we also receive a compile-time error.

## Working of Modulus Operator in C

Since it always discovers a remainder of the two accessible numbers or integers concerning the numerator and denominator, the operation of this type of operator depends on the user's value. Various questions over the years have been formulated in GATE question papers based on the working of the Modulus Operator in C. Remember that we are discussing the residual, not the numerator-denominator pair's quotient. For instance:

• 9% 2 will leave us with a remainder of 1. It's because there is a residual of 1 when we divide 9 by 2, which results in a quotient of 3.
• Similarly, 8% 5 will leave us with 3 as the remaining number. Again, it is because when we divide 8 by 5, the quotient is 1, yet there is still a residual of 3.
• 6% 3 will result in 0 as 3 divides 6 completely, and no remainder is left.

## Calculation of Modulus Operator in C

Let's examine the % operator's internal calculation in some C code currently in use. These calculations are important for the GATE exam as well. If p and q are integers, then the resolution of p% q is p - (p/q) * q. Now, we will assume values for p and q and then apply the modulus operator to them to arrive at the solution.

Example:

If p = 53 and q = 17, then

p % q >> p – (p/q) * q

53 % 17 >> 53– (53/17) * 17

53– (3) * 17

53 – 51

2

Thus, p % q here is 2.

## Example for Modulus Operator in C

Being familiar with the working of the modulus operator, let us understand it better using a sample C code for modulus operation:

#include<stdio.h>

#include<conio.h>

void main()

{

int x, y;

int res; // store the resultant of modulus expression

x = 15;

y = 2;

res = x % y; // define modulus expression

printf(" Modulus returns a remainder: %d", res);

res = y % x; // define modulus expression

printf(" \n Modulus returns a remainder: %d", res);

x= 20;

y = 5;

res = x % y; // define modulus expression

printf(" \n Modulus returns a remainder: %d", res);

getch();

}

The output of the above code after execution will be:

• Modulus returns a remainder: 1
• Modulus returns a remainder: 2
• Modulus returns a remainder: 0

## Exceptional Restrictions of the Modulus Operator in C

So far, we have thoroughly discussed the modulus operator usage in the C programming language, along with the help of examples. However, there are a few restrictions of the modulus operator, which are as follows:

• The % operator cannot be applied to a set of floating numbers (float or double). The compiler will produce a compile-time error if we try to use this operator with any floating-point variables or even constants in C.
• When employing the modulo operator on any two integers, it turns out that the sign of the result is machine-dependent for all the negative operands. It's because the overflow or underflow causes the activity to happen.
 Important GATE Topics Structural Steel Statically Indeterminate RLC Circuit Work Done By A Force Structural Analysis Motion Under Gravity Inductors In Series Dynamic Resistance Method Of Joints Static Resistance

write a comment

## FAQs on Modulus Operator in C Programming

• The operator that returns the remaining value after performing an integer division is the modulus operator, commonly known as the remainder operator. An example of the same is as follows: 7/4 = 1 while the remainder is 3. As a result, 7% 4 = 3.

• The Modulo Operator is the name given to the % symbol. The remainder after dividing the left-hand operand by the right-hand operand is what is returned by performing this operation. To find the remaining part of a division, we use modulus operation or mod operation.

• In many different situations, the modulus operator is helpful. It can be used to rapidly determine whether two numbers are factors of one another as well as to take a randomly produced number and reduce it to a random number with a smaller range.

• The expression "5 mod 2" would evaluate to 1, as 5 divided by 2 has a quotient of 2 and a remainder of 1, but "10 mod 2" would evaluate to 0, as there is nothing to subtract from 10 after multiplying 5 times 2.

• The modulo operation, also abbreviated as "mod" or "%" in many programming languages, is the remainder part when a division takes place. For example, "10 mod 3 = 1," which means 1 is the remainder when you divide 10 by 3.

### GATE & PSU CS

Computer ScienceGATEGATE CSENIELITOther ExamsQuick LinksMock Test GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com