Write a program to Check whether the number is even or odd using switch statement


This is a Java program that determines if a number is odd or even. Here's how it works:

  • First, we import the Scanner class from the java.util package to read user input.
  • We declare an integer variable num and initialize it to 0.
  • We prompt the user to enter a number using System.out.printf method and read the input using Scanner.nextInt method.
  • We use a switch statement to check if the remainder of num divided by 2 is 0 or 1.
  • If the remainder is 0, we print "This is a Even Number".
  • If the remainder is 1, we print "This is a Odd Number".

Source Code

import java.util.Scanner;
public class Odd_Even
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int num = 0;
		System.out.printf("Enter the Number : ");
		num = input.nextInt();
		switch (num % 2)
		{
			case 0:
				System.out.printf("This is a Even Number");
				break;
 
			case 1:
				System.out.printf("This is a Odd Number");
				break;
		}
	}
}

Output

Enter the Number : 23
This is a Odd Number