The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder. These functions give the same values for positive arguments, 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.

You can find the positive value by doing this:

int i = (((-1 % 2) + 2) % 2)

or this:

int i = -1 % 2;
if (i<0) i += 2;

(obviously -1 or 2 can be whatever you want the numerator or denominator to be)

Answer from andrewmu on Stack Overflow
Discussions

Java Tip: modulo with negative numbers
Came across this issue of running a modulo command on a negative number when working on an extension. Java does not handle it correctly. 11 % 7 = 4 >> correct -11 % 7 = -4 >> wrong instead use: Math.floorMod(-11,7) = 3 >> correct ref More on community.appinventor.mit.edu
🌐 community.appinventor.mit.edu
0
2
November 9, 2024
modulo - Best way to make Java's modulus behave like it should with negative numbers? - Stack Overflow
So this question title is much ... numbers. ... Save this answer. Show activity on this post. It behaves as it should: a % b = a - a / b * b; i.e. it's the remainder. You can do (a % b + b) % b. This expression works as the result of (a % b) is necessarily lower than b, no matter if a is positive or negative. Adding b takes care of the negative values of a, since (a % b) is a negative value between -b and 0, (a % b + b) is necessarily lower than b and positive. The last modulo is there in ... More on stackoverflow.com
🌐 stackoverflow.com
Trying to understand modulus with negative numbers. Can't seem to grasp negative numbers. Can someone help?
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. More on reddit.com
🌐 r/computerscience
16
8
June 21, 2024
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
🌐
Medium
medium.com › @jihoo.kang.kor › java-how-the-modulo-operator-is-executed-for-negative-numbers-in-java-7c4888157390
[JAVA] How the modulo operator is executed for negative numbers in JAVA? | by Jihoo Kang | Medium
November 7, 2022 - My expectation was 11/-3 = -4 and -11/3 = -4 since the rounding result of -3.66667 is -4. However, the result is different, and thus JAVA might apply the floor() method without considering any sign of numbers.
🌐
Programming.Guide
programming.guide › java › remainder-modulo-operator-negative-numbers.html
Java: Remainder (modulo) operator with negative numbers | Programming.Guide
In Java, difference between default, public, protected, and private ... Executing code in comments?! ... The sign of the first operand decides the sign of the result. ... You can think of the sign of the second operand as being ignored. Here's a diagram of x % 5 (which is the same as x % -5). The % computes the remainder in a division and should not be confused with the concept of modulo arithmetic.
🌐
Delft Stack
delftstack.com › home › howto › java › mod of negative numbers in java
Mod of Negative Numbers in Java | Delft Stack
February 15, 2024 - Whether using Math.floorMod, custom modulus calculation, or the ternary operator, each method offers unique advantages for handling the modulus of negative numbers in Java.
🌐
Torstencurdt
torstencurdt.com › tech › posts › modulo-of-negative-numbers
Modulo of Negative Numbers
June 28, 2011 - Only two languages take a different stance: Dart and in particular Zig, which distinguishes both cases as @rem(a,b) and @mod(a,b) and errors out on a negative divisor. So if you use the modulo operator to ensure correct bounds for accessing a collection, beware that some languages need a little more diligence. A simple and efficient way is to check the sign. int mod(a, b) { c = a % b return (c < 0) ? c + b : c } As another option, you could also apply the modulo twice. ... Another pitfall to watch out for is when testing whether a number is odd or even using the modulo operator.
Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-manage-modulo-with-negative-numbers-431119
How to manage modulo with negative numbers | LabEx
Learn advanced Java modulo techniques for handling negative numbers, exploring efficient calculation methods and practical programming strategies for managing remainder operations.
🌐
Baeldung
baeldung.com › home › java › core java › the modulo operator in java
The Modulo Operator in Java | Baeldung
September 3, 2025 - Using the modulo operator, we prevent ... next item will overwrite the first. In cases where the dividend is negative, Java returns a negative remainder:...
🌐
Omni Calculator
omnicalculator.com › math › modulo-of-negative-numbers
Modulo Operations with Negative Numbers
November 14, 2023 - In most popular programming languages (such as Java, C, C++, Python), there are separate functions that can calculate the modulus according to both these definitions. There are also languages where only one of these operations is available. In case of doubt, consult the documentation before writing any code. Now, we'll discuss how these two approaches work depending on whether the dividend or the divisor (or both) is negative. ... Floored modulo returns the positive remainder.
🌐
MIT App Inventor
community.appinventor.mit.edu › open source development › extension development
Java Tip: modulo with negative numbers - Extension Development - MIT App Inventor Community
November 9, 2024 - Came across this issue of running a modulo command on a negative number when working on an extension. Java does not handle it correctly. 11 % 7 = 4 >> correct -11 % 7 = -4 >> wrong instead use: Math.floorMod(-11,7) = 3 >> correct ref
🌐
GeeksforGeeks
geeksforgeeks.org › c language › modulus-on-negative-numbers
Modulus on Negative Numbers - GeeksforGeeks
July 23, 2025 - Below is the correct implementation to find remainder of negative number: ... #include <iostream> using namespace std; int main() { int a = -7, b = 5; cout << ((a % b) + b) % b; return 0; } ... #include <stdio.h> int main() { // a positive and b negative. int a = 7, b = -5; printf("%d", a % b); return 0; } ... /*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main(String[] args) { int a = 7; int b = -5; System.out.println(a % b); } } // This code is contributed by ksrikanth0498.
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-mastering-modulo-operator-java
Mastering the Modulo Operator in Java: A Comprehensive Guide - CodingTechRoom
It's important to note the behavior of the modulo operator with negative numbers. In Java, `-5 % 3` results in `-2`. To ensure a non-negative result, additional logic can be employed when necessary.
🌐
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
🌐
javaspring
javaspring.net › blog › java-mod-negative
Understanding Java Modulo Operation with Negative Numbers — javaspring.net
This is different from some other programming languages where the result always has the sign of the divisor (b). In Java, the % operator is used for the modulo operation. Let's look at some code examples to understand how it works with negative ...
🌐
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.

🌐
Coderanch
coderanch.com › t › 401943 › java › isn-mod
% isn't really mod, is it? (Beginning Java forum at Coderanch)
January 3, 2006 - I often see "%" referred to as the "mod" operator, but it isn't really - it is more like a remainder operator, since it gives negative values for negative "dividends.". Ok, this isn't a question so much as a discovery.
🌐
GitHub
github.com › processing › processing › issues › 6016
Modulo with negative number · Issue #6016 · processing/processing
March 28, 2020 - Modulo is not working with value1 as a negative number. println(-1 % 10); The result of this was -1. The expected result was 9.
Author   mitchellpincham
🌐
LeetCode
leetcode.com › problems › subarray-sums-divisible-by-k › discuss › 320139 › Why-Java-gives-negative-values-when-doing-negative-mod › 310475
Why Java gives negative values when doing negative mod?
Can you solve this real interview question? Subarray Sums Divisible by K - Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k. A subarray is a contiguous part of an array. Example 1: Input: nums = [4,5,0,-2,-3,1], k = 5 Output: ...