0
Boas, I’m having some difficulty understanding the rest of the division, I can calculate and get to the value of the rest and understand the formula, but in the code example described below I’m having some difficulty understanding how the rest of the division helps identify whether a number is even or odd. I thank from now on those who can help, I know it’s probably something simple, but I’m really not getting it.
import java.util.Scanner;
public class cond02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
if (N % 2 == 0) {
System.out.println("PAR");
}
else {
System.out.println("IMPAR");
}
sc.close();
}
}
just think that any number divided by by 2 left zero, 2 / 2 left zero 4 / 2 left zero and any odd number divided by 2 will have rest, if continue with the doubt recommend to have a revised in basic division operations.
– André Martins
Note that the rest of the division operation is an operation that applies to numbers whole, thus
m % n
will result in an integer between 0 and n-1, the rest of the division m by n. For the specific case ofm % 2
the result can be either 0 or 1. If 0 means thatm
is divisible by 2 and therefore even and otherwise is odd.– anonimo