VOOZH about

URL: https://www.geeksforgeeks.org/cpp/modulo-operator-in-c-cpp-with-examples/

⇱ Modulo Operator (%) in C/C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Modulo Operator (%) in C/C++

Last Updated : 27 May, 2026

The modulo operator (%) is an arithmetic operator used in C and C++ to find the remainder after dividing one integer by another. It returns the leftover value of an integer division operation.

  • Used to calculate the remainder of division operations between integers.
  • Commonly used in loops, checking even/odd numbers, circular indexing, and hashing.

Output
10 % 3 = 1

Syntax

If x and y are integers, then the expression:

x % y;

pronounced as "x mod y". For example, 10 % 2 will be pronounced as " Ten mod Two".

Example

If 10 % 3 is calculated, the result is 1 because dividing 10 by 3 leaves a remainder of 1. Similarly, 8 % 2 returns 0, which indicates that 8 is an even number.

Return Value of Modulo Operator

  • If y completely divides x, the result of the expression is 0.
  • If x is not completely divisible by y, then the result will be the remainder in the range [0, y-1]
  • (x % y) < (x / 2) .........if (x >= y)
  • (x % y) = x ......... if (x < y)
  • If y is 0, then division by zero is a compile-time error.

To understand operators and their applications in algorithms, check out our Complete C++ Course, where you’ll learn how to use various operators and functions effectively in C++.

Restrictions

The modulo operator has few restrictions or limitations on it. The % modulus operator cannot be applied to floating-point numbers i.e. float or double. If you try to use the modulo operator with floating-point constants or variables, the compiler will produce an error.


Output

Compilation Error in C code :- prog.c: In function 'main':
prog.c:19:16: error:
 invalid operands to binary % (have 'float' and 'float')
 result = x % y;
 ^ 

Modulo Operator for Negative Operands

The sign of the result for the modulo operator is machine-dependent for negative operands, as the action takes as a result of underflow or overflow. 


Output
-3
0
-3

Note: In C and C++, the sign of the result follows the sign of the dividend (left operand).

Example

  • -10 % 3 = -1
  • 10 % -3 = 1

Applications

The modulo operator is widely used in programming for mathematical calculations, looping operations, and data processing tasks.

  • Used to check whether a number is even or odd.
  • Used in cyclic operations like circular queues and clock calculations.
  • Used in hashing algorithms and array indexing.
  • Used in competitive programming for handling large number operations.
Comment
Article Tags: