Modulus Operator in C

We use the % to denote this type of operator (it’s the percentile operator). The modulus operator is added in the arithmetic operators in C, and it works between two available operands. It divides the given numerator by the denominator to find a result. In simpler words, it produces a remainder for the integer division. Thus, the remainder is also always an integer number only. If there is no remainder obtained, then we get the remainder as zero (0).

In this article, we will take a closer look at the working of Modulus Operator in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Syntax of Modulus Operator in C

Let us consider that the variables p and q are integers in a given code. Then, the modulus expression becomes:

p%q

Possibility of Return Values

It will produce a remainder when p gets divided by q. The possible return values will be:

  • If p gets completely divided by q, then the result obtained out of the expression turns out to be zero (0).
  • If q is not able to completely divide p. then the result obtained will be some integer value that is non-zero. In other words, the remainder will be in a range of [1, x-1].
  • If p is 0, then division by zero generates a compile-time error.
  • If p is some number (integer) and q is 0, then also we get a compile-time error.

Working of Modulus Operator in C

The working of this kind of operator happens on the basis of the value that the end-user receives since it always finds a remainder of the two available numbers/ integers with respect to the numerator and denominator.

Remember that we are talking about remainder and not a quotient of the numerator-denominator pair. For example:

  • 9 % 2 will give us 1 as a remainder. It is because when we divide 9 by 2, we get 3 as a quotient, but there remains a remainder of 1.
  • In a similar manner, 8 % 5 will give us 3 as a remainder. It is because when we divide 8 by 5, we get 1 as a quotient, but there remains a remainder of 3.
  • Also, 6 % 3 will give us 0 as a remainder. It is because when we divide 6 by 3, we get 2 as a quotient, but there remains a remainder of 0.

Calculation of Modulus Operator in C

Let us take a closer look at the internal calculation of the % operator in an available code in the C language:

When p and q are integers, then

p % q will get resolved as p – (p/q) * q

Example,

If p = 5 and q = 2, then

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

5 % 2 >> 5 – (5/2) * 2

5 – (2) * 2

5 – 4

1

Thus, p % q here is 1.

If p = 9 and q = 8, then

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

9 % 3 >> 9 – (9/8) * 8

9 – (1) * 8

9 – 8

1

Thus, p % q here is 1.

If p = 8 and q = 5, then

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

8 % 3 >> 8 – (8/5) * 5

8 – (1) * 5

8 – 5

3

Thus, p % q here is 3.

Example of Modulus Operator in C

Let us take a look at a detailed example to understand how the operator is used in any program.

#include <iostream>

using namespace std;

int main(void)

{

int p, q;

int result;

p = 3;

q = 4;

result = p % q;

cout << result << endl;

result = q % p;

cout << result << endl;

p = 4;

q = 2;

result = p % q;

cout<<result;

return 0;

}

The output obtained out of this program would be:

3

1

0

Some Exceptional Restrictions of the Modulo Operator in C language

This type of operator comes with some very unavoidable limitations or restrictions that you must know, such as:

1. One cannot use and apply the % operator on an available set of floating numbers (that is float or double). If we try to use this operator with any floating-point variables or even constants, the compiler will generate a compile-time error.

For instance, the following program will generate an error:

#include <iostream>

using namespace std;

int main()

{

float p, q;

float result;

p = 2.3;

q = 1.5;

result = p % q;

cout << result;

return 0;

}

2. The sign of the result that we obtain with any pair of integers using the modulo operator turns out to be machine-dependent for all the negative operands. It is because the action takes place as a result of the overflow or underflow.

For example, look at the following program:

#include <iostream>

using namespace std;

int main(void)

{

int p, q;

int result;

p = -3;

q = 4;

result = p % q;

cout << result << endl;

p = 4;

q = -2;

result = x % y;

cout << result << endl;

p = -3;

q = -4;

result = x % y;

cout << result;

return 0;

}

The output generated here would be:

-3

0

-3

Note – Different compilers will show different results here. Some will generate -1 for the expression, while others will generate 1.

Practice Problems on Modulus Operator in C

If p and q are integers, then find the remainder for p % q of the following as p – (p/q) * q

1. If p = 3 and q = 2, then

A. 1

B. 2

C. 0

D. 3

Answer – A. 1

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

3 % 2 >> 3 – (3/2) * 2

3 – (1) * 2

3 – 2

1

Thus, p % q here is 1.

2. If p = 7 and q = 4, then

A. 2

B. 0

C. 1

D. 3

Answer – D. 3

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

7 % 4 >> 7 – (7/4) * 4

7 – (1) * 4

7 – 4

3

Thus, p % q here is 3.


FAQs

How does the % operator work?

The working of this kind of operator happens on the basis of the value that the end-user receives since it always finds a remainder of the two available numbers/ integers with respect to the numerator and denominator. Remember that we are talking about remainder and not a quotient of the numerator-denominator pair. For example, 9 % 2 will give us 1 as a remainder. It is because when we divide 9 by 2, we get 3 as a quotient, but there remains a remainder of 1.

In what cases do we get an error from the compiler when using the modulo operator?

If p and q are integers, then for p % q: If p is 0, then division by zero generates a compile-time error. If p is some number (integer) and q is 0, then also we get a compile-time error. Also, one cannot use and apply the % operator on an available set of floating numbers (that is float or double). If we try to use this operator with any floating-point variables or even constants, the compiler will generate a compile-time error.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility CriteriaGATE 2023GATE Admit CardGATE Syllabus for CSE (Computer Science Engineering)GATE CSE NotesGATE CSE Question Paper, and more.

Also Explore,

Leave a Comment

Your Mobile number and Email id will not be published.

*

*