Javatpoint Logo
Javatpoint Logo

Modulus Operator in C/C++

Before understanding about modulus operator, we need to know about the term operator. In programming, the operator symbol tells the compiler to perform a particular operation at a given number based on the passed operation.

It is used to perform different operations (+, -, *, /) on numbers. It helps to enhance the programmer's logical and mathematical ability by using various operators in the program. Except these four basic operators, there are some other operators such as modulus operator (%), scope resolution operator (::), etc.

In this section, we will focus on the modulus operator.

Modulus Operator in C/C++

What is the modulus operator in C and C++?

The modulus operator is a symbol used in various programming languages. It is denoted by the percentage symbol (%). It is a modulus operator that is used in the arithmetic operator. It determines the remainder. In some cases, the remainder may be 0, it means the number is completely divisible by the divisor.

Syntax:

In the above Syntax, a and b are two integers, and the % (Percent) symbol is a modulus operator that divides a by b and returns the remainder.

Return Possibilities of the Modulus Operator

Following are the possibilities when the first number is divided by the second number to return only a remainder value.

  1. If the variable a is completely divisible by the second number (b), it returns zero (0), or the remainder becomes 0.
  2. If the variable a is not completely divisible by the second number (b), it returns a numeric value in the range [1, a - 1]. Or we can say it returns the remainder to a non-zero integer value.
  3. If the first number (a) is non-zero and the second number is 0, it gives an error at compile time.

How does Modulus Operator work in C/C++?

The modulus operator works with two operands received by the end-user. After that, it divides the first number by the second number and determines the remainder. Let's understand the following example that illustrates the functionality of the modulus operator.

Example: When we perform the modulus operator between 8 and 5, means 8 % 5, it returns the remainder 3 because when 8 is divided by 5, it returns 1 as the quotient and 3 as the remainder.

Similarly, 7 % 2 returns 1 as a remainder because when 7 is divided by 2, it returns 3 as quotient and 1 as remainder.

Example 1: Write a program to implement the Modulus Operator in C.

Mode.c

Output:

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

Note: When we divide a float number by another number, it returns the compiled timer error as an invalid operand. Hence, we can say that it does not work with float number.

Program to implement the modulus operator in C++.

Mode3.cpp

Output:

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

Modulus Operator in C++

Mode4.cpp

Output:

Modulus returns a remainder: -1
 Modulus returns a remainder: 2
 Modulus returns a remainder: -1

Chaining of Modulus Operator

We can use the Chaining of Modulus Operator to perform modular division on more than two operands in a single statement. Following is the pseudo-code for the chaining of modulus operator, as given below.

Let's consider the program of Chaining of Modulus Operator to take more than two operands.

Mode5.cpp

Output:

Modulus is: 2





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA