Java Program to Check Even or Odd number

  • Java program to check whether a number is odd or even number using if-else condition and switch statement.

Java program to print odd or even numbers using if-else statement

package com.tcc.java.programs;

import java.util.*;

public class OddOrEvenOne {
    public static void main(String args[]) {
        int num;
  
        Scanner in = new Scanner(System.in);
        System.out.println("Enter an Integer");
        num = in.nextInt();

        if (num%2 == 0)
            System.out.println(num + " is a EVEN Number");
        else
            System.out.println(num + " is ODD Number");
    }
}
Output
Enter an Integer
4
4 is a EVEN Number

Enter an Integer
5
5 is ODD Number

Java program to find od or even number using switch statement

package com.tcc.java.programs;

import java.util.*;

public class OddOrEvenSwitch {
    public static void main(String args[]) {
        int num;

        Scanner in = new Scanner(System.in);
        System.out.println("Enter an Integer");
        num = in.nextInt();
        switch(num%2) {
            case 0:
                    System.out.println(num + " is a EVEN Number");
                    break;
            case 1:
                    System.out.println(num + " is ODD Number")
        }
    }
}
Output
Enter an Integer
2
2 is a EVEN Number
Enter an Integer
7
7 is ODD Number