ADVERTISEMENT
ADVERTISEMENT

Java program to check whether the number is EVEN or ODD using switch statement

Give/input a number, we have to check whether the number is EVEN or ODD using switch statement.
Submitted by Nidhi, on March 03, 2022

Problem Solution:

In this program, we will read an integer number from the user and check given number is EVEN or ODD using a switch statement.

Program/Source Code:

The source code to check whether the number is EVEN or ODD using the switch statement is given below. The given program is compiled and executed successfully.

// Java program to check whether a number is 
// EVEN or ODD using switch statement

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner SN = new Scanner(System.in);

    int number = 0;

    System.out.printf("Enter a positive integer number: ");
    number = SN.nextInt();

    switch (number % 2) {
    case 0:
      System.out.printf("%d is an EVEN number.\n", number);
      break;

    case 1:
      System.out.printf("%d is an ODD number.\n", number);
      break;
    }

  }
}

Output:

Enter a positive integer number: 15
15 is an ODD number.

Explanation:

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we read an integer number from the user using the Scanner class. Then we checked input number is EVEN or ODD. After that, we printed the appropriate message.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.