Java: Verifier si un nombre est pair ou impair

Author:


{filelink=4590}

public class PairOuImpair {
  public static void main(String[] argv) {
    int x = 100;
    if (x % 2 == 0) {
      System.out.println("Pair");
    }
    if (x % 2 != 0) {
      System.out.println("Impair");
    }
    /* 
       Test avec des opérateurs binaires.
       Voir les calculs binaires
     */
    if ((x & 1) == 0) 
     {
      System.out.println("Pair");
    }
    if ((x & 1) != 0) {
      System.out.println("Impair");
    }

  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *