Check value

Java program to find odd or even number using switch statements

Java program to find odd or even number using switch statements

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

Find odd and even number

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

 

Java program to find odd or even number

Find the Even number – Standard method

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

 

Find the Odd number – Standard method

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

 

Find the odd or Even number – Entered by user

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

  • The modular operator uses to find the modulus of the number dividing by 2.
  • Switch statements return either 0 or 1.
  • Then, check the case value with 0 or 1.
  • if the case value is 0, the number will be even and case value 1, the number will be odd.

 

Suggested for you

Datatype and variable in Java language

The operator in Java language

Class and main method in Java

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

 

Python program to display all odd or even numbers 1 to n with label
Find odd or even number using switch statements in C
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago