Skip to content
Related Articles

Related Articles

Modulus on Negative Numbers

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 12 Aug, 2022

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


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.


Output

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;
}


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


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;
}


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


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;
}


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


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.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!