Java ternary, three-way, ? Operator check even odd number

Question

We would like to write an application that reads an integer

Determine and print whether it's odd or even.

Use the remainder operator.

An even number is a multiple of 2.

Any multiple of 2 leaves a remainder of 0 when divided by 2.

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        int x;/*from w  ww  . ja va 2  s . c o m*/

        System.out.print("Enter an integer: ");
        x = input.nextInt();

        //your code here
    }
}



import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        int x;

        System.out.print("Enter an integer: ");
        x = input.nextInt();

        System.out.printf("%d is %s\n", x, (x % 2 == 0 ? "even": "odd"));
    }
}



PreviousNext

Related