Question: (Odd or Even) Write an application that reads an integer and determines and prints whether it’s odd or even. [Hint: 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.]
Java Program to Check Whether a Number is Even or Odd
/*
* Filename: OddEven.java
*
* Description: Exercise 2.25 - Odd or Even
*
* @Author: Bilal Tahir Khan Meo
* Website: https://codeblah.com
*
* =====================================================================================
*/
import java.util.Scanner;
public class OddEven{
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"));
}
}
Method 2 – Program To Check If A Number Is Odd Or Even
/**
*
* @Author: Bilal Tahir khan Meo
* Website: https://codeblah.com
*
* Exercise 2.25 - Odd Or Even
* This Program Determines If A Number Is Odd Or Even
*
*/
import java.util.Scanner;
public class Ex02_25 {
public static void main (String [] args) {
Scanner numbers = new Scanner (System.in);
int num;
int check;
System.out.print ("Enter A Number: ");
num = numbers.nextInt();
check = num%2;
if (check == 0)
System.out.printf ("\nThe Number %d is EVEN.\n", num);
if (check != 0)
System.out.printf ("\nThe Number %d is ODD.\n", num);
}
}