Modulus on Negative Numbers

17 Jun, 2022
What will be the output of the following C program?  
C++
C
C#
#include <iostream>using namespace std; int main() {    int a = 3, b = -8, c = 2;   cout << a % b / c;   return 0;}
#include <stdio.h>int main(){   int a = 3, b = -8, c = 2;   printf("%d", a % b / c);   return 0;}
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
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++
C
C#
#include <iostream>using namespace std; int main() {     int a = 3, b = -8;     cout << a % b;    return 0;}
#include <stdio.h>int main(){   // a positive and b negative.   int a = 3, b = -8;   printf("%d", a % b);   return 0;}
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++
C
C#
#include <iostream>using namespace std; int main(){   // a negative and b positive   int a = -3, b = 8;   cout << a%b;   return 0;}
#include <stdio.h>int main(){   // a negative and b positive   int a = -3, b = 8;   printf("%d", a % b);   return 0;}
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++
C
C#
#include <iostream>using namespace std; int main() {   // a and b both negative   int a = -3, b = -8;   cout << a % b;   return 0;}
#include <stdio.h>int main(){   // a and b both negative   int a = -3, b = -8;   printf("%d", a % b);   return 0;}
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. 
sushantgundla
surbhit7
namalashivacharan123
2019kucp1053
guptavivek0503
sarajadhav12052009
C-Operators
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Function Pointer in C
Multidimensional Arrays in C / C++
Different Methods to Reverse a String in C++
Left Shift and Right Shift Operators in C/C++
std::string class in C++
Unordered Sets in C++ Standard Template Library
rand() and srand() in C/C++
Enumeration (or enum) in C
What is the purpose of a function prototype?