  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# What does Modulo Operator do in C / C++?

Last Updated on March 3, 2023 by Prepbytes Arithmetic operators are mathematical operators that perform basic arithmetic operations such as addition, subtraction, multiplication, division, and modulo. These operators are used to perform mathematical calculations on numerical values and are present in most programming languages, including C/C++, Java, Python, and many others. They are essential for many algorithms and are widely used in various applications, from simple mathematical calculations to complex scientific simulations. The syntax for using these operators varies between programming languages, but the basic operations performed by these operators remain the same. The availability of arithmetic operators allows developers to easily perform mathematical operations on numeric data, making it easier to write programs that perform mathematical calculations.

## What is Modulo Operator?

The modulo operator is a mathematical operator that returns the remainder of a division operation. It is represented by the percentage symbol (%) in most programming languages. The modulo operator is used to divide one number by another and return the remainder as its result.

For example, if we use the modulo operator to divide 10 by 3, we get

``10 % 3 = 1``

This means that 3 goes into 10 a total of 3 times, with a remainder of 1.

In programming, the modulo operator is often used for a variety of purposes, such as wrapping around an array index to ensure that it stays within the bounds of the array, or to find odd or even numbers by checking the remainder of division by 2.

Another important property of the modulo operator is that it is cyclic with a period equal to the divisor. This means that if a and b are positive integers, and m is the modulo, then the expression (a + b) % m is equal to (a % m + b % m) % m.

It’s worth noting that the behavior of the modulo operator depends on the implementation and the programming language. Some programming languages treat the result of the modulo operator as a positive number, while others treat it as a sign of the dividend. In most programming languages, including Python and C, the sign of the result is the same as the sign of the dividend.

### Properties of Modulo Operator

The modulo operator has several important properties that make it useful in a variety of mathematical and computational contexts. Here are some of the most important properties of the modulo operator:

• Cyclic: The result of the modulo operation is always in the range 0 <= x % y < y, where x is the dividend and y is the divisor. This means that the result of the modulo operation is cyclic with a period equal to the divisor.
• Commutative: The modulo operation is commutative, meaning that (a % b) = (b % a) for any positive integers a and b.
• Associative: The modulo operation is associative, meaning that ((a % b) % c) = (a % (b % c)) for any positive integers a, b, and c.
• Distributive: The modulo operation is distributive over addition and subtraction, meaning that (a + b) % c = ((a % c) + (b % c)) % c and (a – b) % c = ((a % c) – (b % c)) % c for any positive integers a, b, and c.
• Transitive: If (a % b) = (c % b), then a = c + nb for some integer n.
• Linear: The modulo operation is linear, meaning that if a = bq + r for some integer q and 0 <= r < b, then a % b = r.

These properties make the modulo operator a useful tool for solving a variety of mathematical and computational problems, such as finding the least common multiple of two numbers, checking if a number is divisible by another number, and wrapping around an array index.

## Working Of Modulo Operator in C / C++

In C and C++, the modulus operator % is used to find the remainder of dividing one integer by another. The syntax of the modulus operator in C and C++ is as follows:

``result = dividend % divisor;``

Here, dividend and divisor are both integer values, and the result is the integer remainder of the division of dividend by the divisor.

For example, the following code calculates the remainder of dividing 17 by 4:

```#include <stdio.h>

int main(void) {
int dividend = 17;
int divisor = 4;
int result = dividend % divisor;
printf("%d", result);
return 0;
}```
```#include <iostream>

int main() {
int dividend = 17;
int divisor = 4;
int result = dividend % divisor;
std::cout << result << std::endl;
return 0;
}```

Output:

``1``

This program will output 1 since 17 divided by 4 is 4 with a remainder of 1.

It’s important to note that the result of the modulus operation in C and C++ will always be an integer, even if the result is not a whole number. In other words, the fractional part of the result is discarded.

It’s also worth noting that the sign of the result of the modulus operation in C and C++ is determined by the sign of the dividend, not the divisor. If the dividend is negative, the result will be negative, even if the divisor is positive. For example,

```#include <stdio.h>

int main(void) {
int dividend = -17;
int divisor = 4;
int result = dividend % divisor;
printf("%d", result);
return 0;
}
```
```#include <iostream>

int main() {
int dividend = -17;
int divisor = 4;
int result = dividend % divisor;
std::cout << result << std::endl;
return 0;
}```

Output:

``-1``

This program will output -1 since -17 divided by 4 is -4 with a remainder of -1.

## Example Of Modulo Operator In C / C++

Here are some examples to demonstrate the implementation of the modulo operator in C and C++:

1. Finding the remainder of dividing two integers:
Below is the C / C++ program

```#include <stdio.h>

int main(void) {
int dividend = 10;
int divisor = 3;
int result = dividend % divisor;
printf("The remainder of %d divided by %d is %d\n", dividend, divisor, result);
return 0;
}
```
```#include <iostream>

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

std::cout << "The remainder of " << dividend << " divided by " << divisor << " is " << result << std::endl;
return 0;
}```

Output:

``The remainder of 10 divided by 3 is 1``

2. Using the modulo operator to find odd or even numbers:
Below is the C/C++ program

```#include <stdio.h>

int main(void) {
int num = 10;
int result = num % 2;
if (result == 0)
{
printf("%d is an even number\n", num);
}
else
{
printf("%d is an odd number\n", num);
}
return 0;
}```
```#include <iostream>

int main()
{
int num = 10;
int result = num % 2;

if (result == 0)
{
std::cout << num << " is an even number" << std::endl;
}
else
{
std::cout << num << " is an odd number" << std::endl;
}

return 0;
}
```

Output:

``10 is an even number``

## Some Exceptional Restrictions of the Modulo Operator

There are a few restrictions and considerations when using the modulo operator that can lead to exceptional or unexpected results:

• Division by zero: The modulo operator in C/C++ is undefined for division by zero, which means it is not possible to calculate the remainder when the divisor is zero.
• Negative Operands: For negative operands, the sign of the result for the modulo operator is machine-dependent, as the action takes as a result of underflow or overflow.
• Floating-point numbers: The modulo operator in C/C++ is only defined for integer values, not floating-point numbers. Attempting to use the modulo operator with floating-point numbers will result in a compile-time error.

These restrictions should be kept in mind when using the modulo operator to ensure that the results are as expected. In some cases, it may be necessary to use alternative techniques, such as using the floor division operator / or rounding functions, to achieve the desired results.

Summary

• The modulo operator (denoted as % in most programming languages) returns the remainder after the division of one number by another. For example, 7 % 3 would be equal to 1 because 7 divided by 3 is 2 with a remainder of 1.
• The result of a modulo operation is always between 0 and the divisor (exclusive). So, in the previous example, the result of 7 % 3 would always be between 0 and 2 (exclusive).
• Modulo operations have several useful properties, such as the fact that they satisfy the distributive property, they are associative and they are compatible with the rules of arithmetic.
• In computer science, modulo is often used for things like wrapping around an array index, testing if a number is odd or even, or for hash functions.
• The behavior of the modulo operator with negative numbers can vary between programming languages, so it’s important to check the documentation for the specific language you’re using. Some programming languages will return a negative remainder, while others will return a positive one.