In this tutorial, we will discuss the concept of the Java program to find odd or even number using switch statements.
In this post, we are going to learn how to find odd or even number using switch statements in Java programming language
when you divide a number by two and if the balance is zero, it is an even number.
when you divided a number by two and if the balance is one, it is an odd number.
Example of even number 2,4,6,8,…..
Example of odd number 1,3,5,7,…..
Here, we will use a modular operator to display odd or even number of the given number
if n%2==0 n is an even number
if n%2==1 n is an odd number
The program will check and display the even number from the given number using switch statements in Java
Program 1
//C program to check whether number is EVEN using switch class CheckEvenOddSwitch{ public static void main (String args[]){ int num1=100; //int num2=111; switch(num1%2){//this will return 0 case 0: System.out.println(num1+" is an Even number"); break; case 1: System.out.println(num2+" is an Odd number"); } } }
When the above code is executed, it produces the following results
100 is an Even number
The program will check and display the odd number from the given number using switch statements
Program 2
//C program to check whether number is ODD using switch class CheckEvenOddSwitch{ public static void main (String args[]){ //int num1=100; int num2=111; switch(num2%2){//this will return 1 case 0: System.out.println(num1+" is a Even number"); break; case 1: System.out.println(num2+" is a Odd number"); } } }
When the above code is executed, it produces the following results
111 is an Odd number
This program allows the user to enter a number and then, the program will check and display the odd or even numbers from the given number using switch statements
//C program to check whether number is EVEN or ODD using switch import java.util.Scanner; class CheckOddEvenswitch2{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the integer number: "); int num=scan.nextInt();//get input from the user for num switch(num%2){//this will return either 0 or 1 case 0: System.out.println(num+" is a Even number"); break; case 1: System.out.println(num+" is a Odd number"); } } }
When the above code is executed, it produces the following results
Case 1
Enter the integer number: 23 23 is a odd number
Case 2
Enter the integer number: 30 30 is a even number
Approach
Suggested for you
Datatype and variable in Java language
Switch statements in Java language
Similar post
C++ program to find odd or even number using switch statements
C program to find odd or even number using switch statements
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…
View Comments
Please provide a java program to find whether the input integer is even or odd
5 ways to check whether the given integer is Even or Odd in Java
https://code4coding.com/5-ways-to-check-whether-the-given-integer-is-even-or-odd-in-java/