Modulus on Negative Numbers
What will be the output of the following C program?
C++
#include <iostream>using namespace std;int main() { int a = 3, b = -8, c = 2; cout << a % b / c; return 0;} |
C
#include <stdio.h>int main(){ int a = 3, b = -8, c = 2; printf("%d", a % b / c); return 0;} |
Java
/*package whatever //do not write package name here */import java.io.*;class GFG { public static void main (String[] args) { int a = 3; int b = -8; int c = 2; System.out.println(a % b / c); }}// This code is contributed by laxmigangarajula03 |
Python3
a = 3;b = -8;c = 2;print(a % (b // c));# This code is contributed by ksrikanth0498. |
C#
using System;public class GFG{ static public void Main () { int a = 3; int b = -8; int c = 2; Console.WriteLine(a % b / c); }}// This code is contributed by sarajadhav12052009 |
Javascript
var a = 3;var b = -8;var c = 2;document.write(a % b / c);// This code is contributed by ksrikanth0498. |
1
% and / have the same precedence and left to right associativity. So % is performed first which results in 3 and / is performed next resulting in 1. The emphasis is, that a sign of a left operand is appended to result in the case of the modulus operator in C.
C++
#include <iostream>using namespace std;int main() { int a = 3, b = -8; cout << a % b; return 0;} |
C
#include <stdio.h>int main(){ // a positive and b negative. int a = 3, b = -8; 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 = 3; int b = -8; System.out.println(a % b); }}// This code is contributed by ksrikanth0498. |
Python3
a=3;b=-8print(a//(b)) |
C#
using System;public class GFG{ static public void Main () { int a = 3; int b = -8; Console.WriteLine(a % b); }}// This code is contributed by sarajadhav12052009 |
Javascript
var a = 3;var b = -8;document.write(a % b );// This code is contributed by ksrikanth0498. |
Output
3
C++
#include <iostream>using namespace std;int main(){ // a negative and b positive int a = -3, b = 8; cout << a%b; return 0;} |
C
#include <stdio.h>int main(){ // a negative and b positive int a = -3, b = 8; 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 = -3, b = 8; System.out.println(a%b); }}// This code is contributed by ksrikanth0498. |
C#
using System;public class GFG{ static public void Main () { int a = -3; int b = 8; Console.WriteLine(a % b); }}// This code is contributed by sarajadhav12052009 |
Javascript
<script>var a = -3;var b = 8;document.write(a % b );// This code is contributed by ksrikanth0498.</script> |
Python3
if __name__ == '__main__': # a negative and b positive a = -3 b = 8 print(a % b) |
Output
-3
But from the definition of the remainder (as stated here https://en.wikipedia.org/wiki/Remainder) it should always be a least positive integer that should be subtracted from a to make it divisible by b (mathematically if, a = QB + r then 0 ≤ r < |b|).
So, in the above example, -3 is not our real remainder because it is negative.
Therefore, in C/C++ language we always find remainder as (a%b + b)%b (add quotient to remainder and again take remainder) to avoid negative remainder.
C++
#include <iostream>using namespace std;int main() { // a and b both negative int a = -3, b = -8; cout << a % b; return 0;} |
C
#include <stdio.h>int main(){ // a and b both negative int a = -3, b = -8; 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=-3,b=-8; System.out.println(a%b); }}// This code is contributed by ksrikanth0498. |
C#
using System;public class GFG{ static public void Main () { int a = -3; int b = -8; Console.WriteLine(a % b); }}// This code is contributed by sarajadhav12052009 |
Javascript
<script>var a = -3;var b = -8;document.write(a % b );// This code is contributed by laxmigangarajula03.</script> |
Output
-3
Anyone can predict the output of a modulus operator when both operands are positive. But when it comes to the negative numbers, different languages give different outputs.
In C language, modulus is calculated as,
a % n = a – ( n * trunc( a/n ) ).
For example,
8 % -3 = 8 – ( -3 * trunc(8/-3) )
= 8 – ( -3 * trunc(-2.666..) )
= 8 – ( -3 * -2 ) { rounded towards zero }
= 8 – 6
= 2
Important Note:
Numerator | Denominator |
|
|
X sign | Y sign | X/Y sign | X%Y sign |
+ | + | + | + |
+ | – | – | + |
– | + | – | – |
– | – | + | – |
From the above table, we conclude that the % operator always considers a sign of a numerator
For more info, please see https://en.wikipedia.org/wiki/Modulo_operation
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.





.gif)

Please Login to comment...