Open In App

Modulus on Negative Numbers

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The modulus operator, denoted as %, returns the remainder when one number (the dividend) is divided by another number (the divisor).

Modulus of Positive Numbers

Problem: What is 7 mod 5?
Solution: From Quotient Remainder Theorem, Dividend=Divisor*Quotient + Remainder
7 = 5*1 + 2, which gives 2 as the remainder.

Thus, to find 7 mod 5, find the largest number that is less than or equal to 7 and divisible by 5. We get 5 as that number and in order to get the remainder we subtract 5 from 7, which gives 2 that is the remainder.

This gives the definition of the remainder,

A remainder is least positive integer that should be subtracted from a to make it divisible by b (mathematically if, a = q*b + r then 0 ? r < |b|), where a is dividend, b is divisor, q is quotient and r is remainder.

Modulus of Negative Numbers

Problem: What is -7 mod 5?
Solution: To find -7 mod 5, we firstly find the largest number that is less than or equal to -7 and divisible by 5. That number is -10. Now, in order to get remainder subtract -10 from -7, which gives 3 that is the remainder.

Remainder can also be computed from the above definition. Since, remainder is least positive integer that should be subtracted from dividend to make it divisible by divisor. Thus the least positive integer that should be subtracted from -7 to make it divisible by 5 is 3.
Hence the remainder comes out to be 3.

Modulus Operation in Different Programming Languages:

In programming languages, the sign of remainder depends upon sign of divisor and dividend. Let’s consider the following cases:

1. When Dividend is negative and Divisor is positive

Let’s see the result of -7 mod 5 in different programming languages:

C++




#include <iostream>
using namespace std;
 
int main()
{
 
    int a = -7, b = 5;
    cout << a % b;
    return 0;
}


C




#include <stdio.h>
int main()
{
    int a = -7, b = 5;
    printf("%d", a % b);
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = -7;
        int b = 5;
      
        System.out.println(a % b );
    }
}


Python3




a = -7
b = 5
 
print(a % b)


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int a = -7;
        int b = 5;
 
        Console.WriteLine(a % b);
    }
}


Javascript




var a = -7;
var b = 5;
 
document.write(a % b);


Output

-2

Note: The python program gives 3 as the remainder, meanwhile the other programming languages (C/C++) gives -2 as the remainder of -7 mod 5. The reason behind this is Python uses floored division to find modulus.

As we know that Remainder = Dividend – (Divisor * Quotient) and Quotient can be computed from Dividend and Divisor. To find the quotient there are two methods, which determine the sign of the remainder.

Floored division:

In this method, Quotient is determined by the floor division of Dividend and Divisor. It rounds off the quotient to the nearest smallest integer. So remainder can be computed from the following expression:

r = a ? b* floor(a/b), where r, a, b are Remainder, Dividend and Divisor respectively.

Example:

=> -7 % 5 = -7 – 5 *( floor(-7/5) )
=  -7 – 5*( floor(-1.4) )
=  -7 – 5*( -2)
= -7+10
= 3

In Python, floor division is used to determine the remainder.

Truncated division:

In this method, Quotient is determined by the truncate divison of Dividend and Divisor. It rounds off the quotient towards zero. So remainder can be computed from the following expression:

r = a ? b* trunc(a/b), where r, a, b are Remainder, Dividend and Divisor respectively.

Example:

=> -7 % 5 = -7 – 5 *( trunc(-7/5) )
=  -7 – 5*( trunc(-1.4) )
=  -7 – 5*( -1)
= -7+5
= -2

In C/C++, truncate division is used to determine the remainder

Thus, In programming languages which uses truncate divison to find the remainder, we always find remainder as (a%b + b)%b (add quotient to remainder and again take remainder) to avoid negative remainder and get the correct result.

Below is the correct implementation to find remainder of negative number:

C++




#include <iostream>
using namespace std;
 
int main()
{
 
    int a = -7, b = 5;
    cout << ((a % b) + b) % b;
    return 0;
}


C




#include <stdio.h>
int main()
{
    int a = -7, b = 5;
    printf("%d", ((a % b) + b) % b);
    return 0;
}


Output

3

2. When Divisor is negative and Dividend is positive:

Let’s see the result of 7 mod -5 in different programming languages:

C++




#include <iostream>
using namespace std;
 
int main()
{
    int a = 7, b = -5;
    cout << a % b;
    return 0;
}


C




#include <stdio.h>
int main()
{
    // a positive and b negative.
    int a = 7, b = -5;
    printf("%d", a % b);
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = 7;
        int b = -5;
        System.out.println(a % b);
    }
}
 
// This code is contributed by ksrikanth0498.


Python3




a = 7
b = -5
print(a % b)


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int a = 7;
        int b = -5;
 
        Console.WriteLine(a % b);
    }
}
 
// This code is contributed by sarajadhav12052009


Javascript




var a = 7;
var b = -5;
document.write(a % b );
 
// This code is contributed by ksrikanth0498.


Output

2

Floored division:

The sign of remainder is negative using floor division to determine remainder when only divisor is negative.

=> 7 % -5 = 7 – (-5) *( floor(-7/5) )
=  7 + 5*( floor(-1.4) )
=  7 +5*( -2)
= 7-10
= -3

Thus, In above code python code gives -3 as remainder because it uses floored division to find modulus.

Truncated division:

The sign of remainder is positive using truncate division to determine remainder when only divisor is negative.

=> 7 % -5 = 7 – (-5) *( trunc(-7/5) )
=  7 + 5*( trunc(-1.4) )
=  7 + 5*( -1)
= 7 – 5
= 2

Thus, the C/C++ code gives 2 as remainder because it uses truncate divison to find modulus.

3. When Divisor is negative and Dividend is positive:

Let’s see the result of -7 mod -5 in different programming languages:

C++




#include <iostream>
using namespace std;
 
int main() {
   // a and b both negative
   int a = -7, b = -5;
   cout << a % b;
   return 0;
}


C




#include <stdio.h>
int main()
{
   // a and b both negative
   int a = -7, b = -5;
   printf("%d", a % b);
   return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
  public static void main (String[] args) {
    int a=-7,b=-5;
    System.out.println(a%b);
  }
}
 
// This code is contributed by ksrikanth0498.


Python3




if __name__ == '__main__':
  a = -7
  b = -5
  print(a % b)


C#




using System;
 
public class GFG{
 
    static public void Main ()
    {
        int a = -7;
        int b = -5;
       
        Console.WriteLine(a % b);
    }
}
 
// This code is contributed by sarajadhav12052009


Javascript




<script>
var a = -7;
var b = -5;
document.write(a % b );
 
// This code is contributed by laxmigangarajula03.
</script>


Output

-2

Floored division:

The sign of remainder is negative using floor division to determine remainder when both divisor and dividend is negative.

=> -7 % -5 = -7 – (-5) *( floor(-7/-5) )
=  -7 + 5*( floor(1.4) )
=  -7 +5*( 1)
= -7+5
= -2

Truncated division:

The sign of remainder is negative using truncate division to determine remainder when both divisor and dividend is negative.

=> 7 % -5 = 7 – (-5) *( trunc(-7/-5) )
=  7 + 5*( trunc(1.4) )
=  7 + 5*( 1)
= -7 + 5
= -2

Related Articles



Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads