If i%n is j, then (i+1) will either be j+1 or 0 (if j == n-1). Similarly, (i-1) % n will either be j-1 or n-1 (if j==0). i | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | i%3 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 If -1%3 were 1, there'd be a break in the pattern when going from -1 to 0. PS: Note that there is a not-insignificant number of languages where -1%3 is -1, which does introduce a break in the pattern, but at least it's still an ascending sequence until it wraps it around. Answer from sepp2k on reddit.com
🌐
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
Discussions

How is negative modulus of a number calculated?
Given nonnegative integers m, n, and m <= n: -m = n - m (mod n) i.e. (-m % n = n - m). More on reddit.com
🌐 r/learnmath
10
5
October 16, 2019
what is the modulo of negative number?
https://www.learncpp.com/cpp-tutorial/5-3-modulus-and-exponentiation/ The modulus operator can also work with negative operands. x % y always returns results with the sign of x. However this may not be true with an older compiler. Before some time, I forget exactly when, the result was implementation defined. Instead of dealing with negative numbers, if your x is negative then make it positive, reverse it, and make the answer negative. Edit edit : the question i was doing has size limit of [-231 to 231 -1] Then use int64_t throughout. You'll need to do this anyway since the majority of numbers in that range cannot be reversed and still fit in a 32-bit int. More on reddit.com
🌐 r/Cplusplus
5
1
February 21, 2023
Blueprint modulo node doesn't handle negatives? - Programming & Scripting - Epic Developer Community Forums
E.g. -1 % 4 does not return 3, it returns -1. Can someone explain why this is desired behavior? I want to iterate through an array backwards and wrap the index counter around to the last index of the array using modulo. This is pretty common desired behavior of modulo. More on forums.unrealengine.com
🌐 forums.unrealengine.com
1
April 14, 2015
math - Mod in Java produces negative numbers - Stack Overflow
These functions give the same values ... but the modulus always returns positive results for negative input, whereas the remainder may give negative results. There's some more information about it in this question. ... Sign up to request clarification or add additional context in comments. ... @Cachapa please provide an example to support that statement. I believe the OP's solution ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › c language › modulus-on-negative-numbers
Modulus on Negative Numbers - GeeksforGeeks
July 23, 2025 - The sign of remainder is negative ... 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 ...
🌐
Torstencurdt
torstencurdt.com › tech › posts › modulo-of-negative-numbers
Modulo of Negative Numbers
June 28, 2011 - The modulo operator returns the remainder of a division. But things get a little more tricky when you throw negative numbers into the mix.
🌐
Medium
medium.com › @krmayank › modulus-of-a-negative-number-a18decf3773e
Modulus of a Negative Number. It's hard to avoid the topic of modular… | by Mayank Kumar | Medium
October 7, 2020 - My knee jerk reaction was, this should be -1. Lets dig deeper, on why python returns 6. ... In doing an integer division of 15 or -15 by 7, we are truncating the decimal or rounding down. Let's take a slightly mathematical approach to modular arithmetic. If n mod b = r, and n/b =q, then we can write n=q X b +r ... This was simple. Let's go back to our negative number example
Find elsewhere
🌐
Omni Calculator
omnicalculator.com › math › modulo-of-negative-numbers
Modulo Operations with Negative Numbers
November 14, 2023 - 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 an equivalence class, i.e., the whole set of numbers that give the same remainder r when divided by n. In this set, we choose one number as the representative. Most often, this role is played by the remainder of the Euclidean division, which happens to be the smallest non-negative number in the equivalence class.
🌐
Physics Forums
physicsforums.com › mathematics › general math
Can we have modulo a negative number? • Physics Forums
October 1, 2018 - Modulo operations can indeed be defined with negative numbers, but they are often overlooked because they do not provide new insights compared to their positive counterparts. The equivalence classes formed by modulo n and -n are the same, making ...
🌐
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. So 1 % 3 = 1 2 % 3 = 2 3 % 3 = 0 4 % 3 = 1 5 % 3 = 2 ...
🌐
Quora
quora.com › How-does-a-negative-number-modulus-a-positive-number-work
How does a negative number modulus a positive number work? - Quora
2) Apply clock arithmetic (add modulus) if remainder is negative (I show with -> symbol) to get into primary range. Examples: 4 mod 2 = 0; -4 mod 2 = 0; 3 mod 2 = 1; -3 mod 2 = -1 -> 1; ...
🌐
YouTube
youtube.com › caveman chang | algebra teacher
Modular Arithmetic with Negative Numbers - YouTube
Hello there! For today, I will continue the topic of modular arithmetic but this time it will be with negative numbers. There is a slight difference between ...
Published   April 27, 2023
🌐
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.
🌐
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.

🌐
Wikipedia
en.wikipedia.org › wiki › Modulo
Modulo - Wikipedia
3 weeks ago - Although typically performed with ... of n is 0 to n − 1. a mod 1 is always 0. When exactly one of a or n is negative, the basic definition breaks down, and programming languages differ in how these values are defined....
🌐
Unreal Engine
forums.unrealengine.com › development › programming & scripting
Blueprint modulo node doesn't handle negatives? - Programming & Scripting - Epic Developer Community Forums
April 14, 2015 - E.g. -1 % 4 does not return 3, it returns -1. Can someone explain why this is desired behavior? I want to iterate through an array backwards and wrap the index counter around to the last index of the array using modulo. This is pretty common desired behavior of modulo.
🌐
Unacademy
unacademy.com › question & answer › mathematics questions › is modulus always positive?
Is Modulus Always Positive?
August 26, 2022 - In mathematics, the modulus of x is equal to |x| times x. Where x can take on either a positive or negative value or even zero.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-perform-modulo-with-negative-values-in-python
Perform Modulo with Negative Values in Python - GeeksforGeeks
The modulo operator (%) gives remainder after division. With negative numbers, different programming languages handle result differently, which often causes confusion. This rule explains why -5 % 4 gives 3 instead of -1. Understanding this behavior ...
Published   December 23, 2025