Helps to understand the Rest of Division

Asked

Viewed 102 times

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();
    }
}
  • 1

    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.

  • 1

    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 of m % 2 the result can be either 0 or 1. If 0 means that m is divisible by 2 and therefore even and otherwise is odd.

2 answers

1


Look, think about it this way: mathematically, when one number is divisible by another, the rest of the division is 0. For example: 10/2 = 5 , and rest = 0. Now, 11/2 = 5 and rest = 1. If the number is even, it is divisible by 2, already an odd number does not. So, if the result of (N % 2 == 0), as in the example of (10/2), is 0, it is even. Otherwise (as in the example of 11/2), it is odd.

0

I will try to explain to you as briefly as possible:

Every PAR number that is divided by 2 will always have 0 rest, otherwise the number is odd.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.