Open In App

Modulus Operator in Programming

The modulus operator, often represented by the symbol '%', is a fundamental arithmetic operator used in programming languages to find the remainder of a division operation between two numbers. It returns the remainder of dividing the first operand by the second operand.

What is the Modulus Operator?

The modulus operator, denoted by the symbol "%", is a mathematical operation that calculates the remainder of the division between two numbers. It returns the remainder left over after dividing the first operand by the second operand. For instance, in the expression a % b, where a and b are integers, the result is the remainder when a is divided by b.

Basic Syntax of Modulus Operator:

The basic syntax of the modulus operator is straightforward:

result = a % b

Where 'a' is the dividend and 'b' is the divisor. The result will be the remainder after dividing 'a' by 'b'.

How Modulus operator works?

Given two integers a (dividend) and b (divisor):

Mathematically, we can express this as:

a=q⋅b+r 

Where:

The modulus operator yields r, the remainder, when a is divided by b.

Properties of Modulus operator:

  1. Range of Results:
    • The result of a % b will always be in the range [0, b-1].
    • If a is positive and b is positive, the result will be positive.
    • If a is negative or b is negative, the sign of the result depends on the programming language's convention.
  2. Division by Zero:
    • Division by zero is undefined in mathematics. Similarly, the modulus operator by zero is usually undefined or throws an error in programming languages.
  3. Equality to Dividend:
    • If a is less than b, a % b will be equal to a.

Example to show Modulus operator:

Modulus Operator in C:

Here are the implementation of Modulus Operator in C language:

#include <stdio.h>

int main() {
    int result = 10 % 3;
    printf("%d\n", result);  // Output: 1
    return 0;
}

Output
1



Modulus Operator in C++:

Here are the implementation of Modulus Operator in C++ language:

#include <iostream>

int main() {
    int result = 10 % 3;
    std::cout << result << std::endl;  // Output: 1
    return 0;
}

Output
1



Modulus Operator in Java:

Here are the implementation of Modulus Operator in java language:

public class Main {
    public static void main(String[] args) {
        int result = 10 % 3;
        System.out.println(result);  // Output: 1
    }
}

Output
1



Modulus Operator in Python:

Here are the implementation of Modulus Operator in python language:

result = 10 % 3
print(result)  # Output: 1

Output
1



Modulus Operator in C#:

Here are the implementation of Modulus Operator in C# language:

using System;

class Program {
    static void Main(string[] args) {
        int result = 10 % 3;
        Console.WriteLine(result);  // Output: 1
    }
}

Output
1



Modulus Operator in Javascript:

Here are the implementation of Modulus Operator in javascript language:

let result = 10 % 3;
console.log(result);  // Output: 1

Output
1



Uses or Application of Modulus Operator:

The modulus operator is commonly used in programming for various tasks, including:

Performance Considerations of Modulus Operator:

Conclusion:

The modulus operator in programming calculates the remainder of a division operation. It's denoted by the '%' symbol. When applied to two numbers, it returns the remainder after dividing the first number by the second. This operator is commonly used in tasks like checking for divisibility, cycling through a range of values, and implementing algorithms requiring periodic behavior or cyclic patterns.

Article Tags :