Let's say I have (-5) mod 8.
I tried it in both languages Java and C, and they gave me a -5 result when I was expecting 3.
Why is this happening? Can a modulus be negative? And what should I change to get the correct result?
Java code
public class Example {
public static void main(String[] args) {
int x;
x = -5%8;
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
C code
int main(){
int x;
x = -5%8;
printf("%d", x); …Run Code Online (Sandbox Code Playgroud)