C99 requires that when a/b is representable:

(a/b) * b + a%b shall equal a

This makes sense, logically. Right?

Let's see what this leads to:


Example A. 5/(-3) is -1

=> (-1) * (-3) + 5%(-3) = 5

This can only happen if 5%(-3) is 2.


Example B. (-5)/3 is -1

=> (-1) * 3 + (-5)%3 = -5

This can only happen if (-5)%3 is -2

Answer from ArjunShankar on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ modulus-on-negative-numbers
Modulus on Negative Numbers - GeeksforGeeks
July 23, 2025 - 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...
Discussions

Blank output when entering negative numbers - Forum
Hi we've been given the task to ... has been inputted by the user is an even or odd number and if its also a positive or negative number or a zero. Down below is the code that i made but im having trouble since the program won't put the expected output when entering a negative number. ... % (modulus operator) is weird on negative numbers. It isn't quite the same as the mathematical concept of (n mod ... More on cplusplus.com
๐ŸŒ cplusplus.com
August 18, 2021
modular arithmetic - How can I find a mod with negative number? - Mathematics Stack Exchange
$\begingroup$ There are different interpretations of modulus torstencurdt.com/tech/posts/modulo-of-negative-numbers $\endgroup$ More on math.stackexchange.com
๐ŸŒ math.stackexchange.com
March 9, 2017
modulo - Modulus with negative numbers in C++ - Stack Overflow
Plz help about the right place to keep the Modulus ... @Alex1985 it wouldn't make any difference if the % operator always returned a positive value, but since it sometimes gives a negative result, signed variables should be used. ... Does either ANSI C or ISO C specify what -5 % 10 should be?, Modulo operation with negative numbers, Why is the behavior of ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
September 24, 2003
modular arithmetic - Modulus of negative numbers - Mathematics Stack Exchange
I had a doubt regarding the โ€˜modโ€™ operator So far I thought that modulus referred to the remainder, for example $8 \mod 6 = 2$ The same way, $6 \mod 8 = 6$, since $8\cdot 0=0$ and $6$ remains. ... More on math.stackexchange.com
๐ŸŒ math.stackexchange.com
August 15, 2018
๐ŸŒ
Reddit
reddit.com โ€บ r/cplusplus โ€บ what is the modulo of negative number?
r/Cplusplus on Reddit: what is the modulo of negative number?
February 21, 2023 -
lass Solution {
public:
    int reverse(int x) {

       int ans=0;

       while(x!=0)
       {
           int i= x%10;
           x=x/10;
           
           if(ans>INT_MAX/10||ans<INT_MIN/10)
           {return 0;}
           ans =ans*10 +i;
       } 
       return ans;
    }
};

this is the code to reverse the integer number , so 123 will be 321 and -123 will be -321, but i am not understanding the negative number part, what i know is % of negative number is positive

like for example:

take x= -123 in above program

-123%10 = 3 , -123/10=-12 ;

-12%10 = 2, -12/10 = -1;

-1 % 10 = ?? , here i am confused , if it is 1 which is positive like above modulo then the answer will be wrong and if it will be -1 then it is not making sense to me, please someone explain, thanks.

edit : the question i was doing has size limit of [-2^31 to 2^31 -1] , so i can't convert negative number to positive then again convert into negative , because a boundary condition of one number because of ' -1' in positive integer side causing error.

so i look up solution , and the above code worked, but i didn't understand the negative number part.

๐ŸŒ
IncludeHelp
includehelp.com โ€บ c โ€บ modulus-on-negative-numbers.aspx
Modulus on negative numbers in C language
December 23, 2025 - In C language, the modulus operator % behaves differently with negative numbers compared to pure mathematical definitions. When one or both operands are negative, the result depends on how C defines integer division, which truncates toward zero.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ modulus-of-negative-numbers-in-c
Modulus of Negative Numbers in C
July 30, 2019 - So % is working at first, so a % b is generating 7, now after dividing it by c, it is generating 3. Here for a % b, the sign of left operand is appended to the result. Let us see it more clearly. ... If we interchange the sign of a and b, then it will be like below. ... Similarly if both are negative, then also the result will be negative.
๐ŸŒ
Lemoda
lemoda.net โ€บ c โ€บ modulo-operator
Negative numbers and the modulo operator in C
August 8, 2014 - This is an example C program illustrating the behaviour of C's modulo/remainder operator (%) for negative numbers. The modulo operator is not mathematically correct, since it turns negative numbers into negative numbers.
๐ŸŒ
Quora
quora.com โ€บ How-does-the-modulo-operation-work-with-negative-numbers-and-why
How does the modulo operation work with negative numbers and why? - Quora
Answer (1 of 9): Think of it like moving a hand around a clock, where every time we get a multiple of N, weโ€™re back at 0. So, take mod 3 (in C and Python, itโ€™s n % 3)โ€ฆ Starting at N=0 and going forward, itโ€™s 0,1,2,0,1,2,0,1,2โ€ฆ forever.
Find elsewhere
๐ŸŒ
Omni Calculator
omnicalculator.com โ€บ math โ€บ modulo-of-negative-numbers
Modulo Operations with Negative Numbers
November 14, 2023 - Here we address the non-obvious ... for negative numbers. In particular, we discuss how this works in programming languages. If you're new to the modulos, check out our dedicated modulo calculator. Recall that the modulo operator a mod n returns the remainder r of the division of a by n. More formally, in number theory, the result of the modulus operator is ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 279553
Blank output when entering negative numbers - Forum
August 18, 2021 - Hello shunin, When all fails use a calculator that you are sure of and as Ganado pointed out on my Windows calculator -9 % 2 evaluates to -1. I fine it useful when you have many if/else if statements to work with. Andy ... another way to look... if the most significant bit is set, its negative, and if the least bit is set, its odd.
Top answer
1 of 3
70

It's $3$ because $-11 = 7(-2) + 3$.

Another way to see this is to take $-11$ and keep adding $7$ to it until you get a positive number. This works because, if you're working modulo $7$, then adding $7$ is the same as not changing the number (modulo $7$). So:

$-11 + 7 \equiv -11 \pmod 7$, and $-11 + 7 = -4$. Therefore $-4 \equiv -11 \pmod 7$. Well, we're still negative. Let's do it again:

$-4 + 7 \equiv -11 \pmod 7$, and $-4 + 7 = 3$. Therefore, $3 \equiv -11 \pmod 7$.

Or, equivalently, $-11 \equiv 3 \pmod 7$.


How do we know to use $-2$? Let's recall how it works with positives first.

If you want to evaluate $31 \pmod 7$, you first recognize that $31 = 28 + 3 = 7 \cdot 4 + 3$. Therefore $31 \equiv 3 \pmod 7$. What did we do here? We found the largest multiple of $7$ that's less than or equal to $31$.

Alternatively, with division, you can evaluate $31/7 \approx 4.429$. The largest integer less than or equal to this is $4$. Therefore $31 = 7 \cdot 4 + \text{some number}$, where your goal is to determine what $\text{some number}$ is.

This same exact process applies for negative numbers.

If you want to evaluate $-11 \pmod 7$, you need the largest multiple of $7$ that's less than or equal to $-11$. This is $-14$. And $-14 + 3 = -11$, therefore your answer is $3$.

Alternatively, with division, you can evaluate $-11/7 \approx -1.571$. The largest integer less than or equal to this is $-2$. Therefore $-11 = 7 \cdot (-2) + \text{some number}$, where your goal is to determine what $\text{some number}$ is.

2 of 3
5

As others have said, it is because $โˆ’11 = 7(โˆ’2) + 3$

However, I believe all other answers failed to mention that you need(ed) to define the division algorithm FIRST.

The division algorithm is defined as Euclidean division, where given two integers a and b, with b โ‰  0, there exist unique integers q and r such that

$a = bq + r$ and $0 \leq r < |b|$

therefore, $r$ MUST always be positive and numbers modulo fit into this definition therefore, the mod of a number is never negative.

๐ŸŒ
YouTube
youtube.com โ€บ neso academy
Modulus of Negative Numbers - YouTube
Discrete Mathematics: Modulus of Negative NumbersTopics discussed:1) Finding the modulus of a negative number.2) Calculating -6 mod 5.3) Finding the mod usin...
Published ย  October 7, 2021
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ modulo-operations-in-programming-with-negative-results
Modulo Operations in Programming With Negative Results - GeeksforGeeks
July 23, 2025 - In programming, the modulo operation ... discusses when and why the modulo operation yields a negative result. ... In C, 3 % 2 returns 1....
๐ŸŒ
Cach3
tutorialspoint.com.cach3.com โ€บ modulus-of-negative-numbers-in-c.html
Modulus of Negative Numbers in C
Let us see it more clearly. #include<stdio.h> int main() { int a = 7, b = -10; printf("Result: %d", a % b); } ... If we interchange the sign of a and b, then it will be like below.
๐ŸŒ
Blogger
lessonsincoding.blogspot.com โ€บ 2012 โ€บ 03 โ€บ modulus-with-negative-numbers-in-c-c.html
Lessons In Coding: Modulus With Negative Numbers in C & C++
December 22, 2023 - If their signs are opposite (one negative, one positive), the quotient is negative. What about the modulus? Itโ€™s easy to think the same process applies, but does it? No. To calculate r, use the division equation for known values of a, b and ...
Top answer
1 of 5
45

The thing is that the % operator isn't the "modulo operator" but the "division remainder" operator with the following equality

(a/b)*b + a%b == a    (for b!=0)

So, if in case your integer division rounds towards zero (which is mandated since C99 and C++11, I think), -5/4 will be -1 and we have

(-5/4)*4 + -5%4 == -5
  -1  *4    -1  == -5

In order to get a positive result (for the modulo operation) you need to add the divisor in case the remainder was negative or do something like this:

long mod(long a, long b)
{ return (a%b+b)%b; }
2 of 5
11

Using % a second time in @sellibitze's and @liquidblueocean's answers probably won't be as slow as % tends to be in general, because it boils down to either one subtraction of b or none. Actually, let me just check that...

int main(int argc, char **argv) {
    int a = argc;    //Various tricks to prevent the
    int b = 7;       //compiler from optimising things out.
    int c[10];       //Using g++ 4.8.1
    for (int i = 0; i < 1000111000; ++i)
        c[a % b] = 3;
        //c[a < b ? a : a-b] = 3;
    return a;
}

Alternatively commenting the line with % or the other line, we get:

  • With %: 14 seconds

  • With ?: 7 seconds

So % is not as optimised as I suspected. Probably because that optimisation would add overhead.

Therefore, it's better to not use % twice, for performance reasons.

Instead, as this answer suggests and explains, do this:

int mod(int k, int n) {
    return ((k %= n) < 0) ? k+n : k;
}

It takes a bit more work if you want it to work properly for negative n too, but that's almost never necessary.

Top answer
1 of 4
5

For every integer $m$ there are are unique integers $q$ and $r$ where $m = 6q + r$ and $0 \le r < 6$. In the case of $m = -8$ the $q= -2$ and $r = 4$ because $-8 = 6(-2) + 4$.

If the remainder has to be an integer $r$ so that $0 \le r < n-1$ then $- 8 \equiv 4 \mod 6$ because $-8 + 2* 6 = 4$. $4$ is the ONLY possible integer between $0$ and $6$ that you can get to from $8$ by adding or subtracting multiples of $6$.

2 of 4
2

Not sure how familiar with modular arithmetic you are, but deriving a few basic results and appealing directly to definitions, those results become much more obvious:

Proposition 1.1: For any two integers $a,b$, with $a \gt 0$, there exist integers $q,r$ such that $$ b=qa +r , \qquad 0 \leq r \lt a.$$

Proof:

Consider the rational number $\frac{b}{a}$. There exists a unique integer $q$ such that $$q \leq \frac{b}{a} \lt q +1$$ $$\implies qa \leq b \lt qa + a$$ $$\implies 0 \leq b - qa \lt a,$$ and, letting $r=b - qa $, the result follows.

$\square$

Definition 1.2: Let $a,b \in \mathbb{Z}$. We say $a$ divides $b$, if, for some integer $c$, $$b=ac.$$

$\quad$

Definition 1.3: Let $m$ be a positive integer. For any $a,b \in \mathbb{Z}$, if $m$ divides $a-b$, we write $a \equiv b \pmod{m}$.

$\quad$

Proposition 1.4: Every integer is congruent to exactly one of the integers $0,1,2 \cdots, m-1$ $\pmod{m}$.

Proof:

Note that $$a \equiv b \pmod{m} \iff a-b=qm,$$ for some integer $q$, and so Proposition 1.4 follows immediately from Proposition 1.1.

$\square$

Evaluating the example in your question, by Proposition 1.4, $-8$ is congruent to exactly one of the integers $0,1, 2,3,4,5, \pmod{6}$.

Now, it is clear that $-8=-2 \cdot 6 + 4$ and so $$-8 \equiv 4 \pmod{6},$$ or, in your notation $$-8\pmod{6}=4.$$

๐ŸŒ
Reddit
reddit.com โ€บ r/computerscience โ€บ trying to understand modulus with negative numbers. can't seem to grasp negative numbers. can someone help?
r/computerscience on Reddit: Trying to understand modulus with negative numbers. Can't seem to grasp negative numbers. Can someone help?
June 21, 2024 -

In the below examples, the modulus with the positive integers makes sense. I've almost been programmed my whole life with division and remainders. However, the negative numbers don't make sense. I've looked at difference formulas, but I can't seem to make them work in my head or on paper. (Using the Window calculator for results)

-4 mod 3 = 2 but 4 mod 3 = 1
-5 mod 3 = 1 but 5 mod 3 = 2
๐ŸŒ
Tpoint Tech
tpointtech.com โ€บ modulo-of-negative-numbers-in-c
Modulo of Negative Numbers in C - Tpoint Tech
March 17, 2025 - In this tutorial, we will look at a few examples of modulus on negative numbers in C.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 101643-mod-negatives.html
Mod with negatives
April 11, 2008 - I believe the C modulus operator is undefined for negative numbers. It's not undefined anymore in C99. They finally decided that the mod operator always obeys the following: ... -9 / 26 = 0 with a remainder of -9. I don't know why Python gives 17 (26 + -9?) as the answer.