Java Menu Driven Program to Check Positive Negative or Odd Even Number


A number is said to be a positive number if it is greater than 0 and if it is less than 0 then it is called a negative number. Negative number is followed by a minus sign (-) to identify it as a negative number.

A number is said to be an even number if it is divisible by 2 else it is called an odd number. In this article we will see how to check if a number is positive, negative or even, odd by using Java programming language. We will be implementing the application using a switch case.

To show you some instances 

Instance-1

Suppose the input number is 12.
Checking it as a positive or negative number, it will result 12 as a positive number as it is greater than 0.

Instance-2

Suppose the input number is -12.
Checking it as a positive or negative number, it will result -12 as a negative number as it is less than 0.

Instance-3

Suppose the input number is 2022.
Checking it as an even or odd number, it will result 2022 as an even number as it is divisible by 2.

Instance-4

Suppose the input number is 7.
Checking it as an even or odd number, it will result 7 as an odd number as it is not divisible by 2.

Syntax

To perform operations like checking if a number is positive or negative, or it is even or odd we use if else statement with some basic logic for each part mentioned above.

Following is the syntax for “if else statement”

if(condition) {
   //code to be executed
}

Algorithm

Step-1 − Ask the user to input the desired number.

Step-2 − Display the menu.

Step-3 − Ask the user to enter their choice.

Step-4 − Use a switch case to go to the choice and perform the operation.

Step-5 − Print the result.

Let’s see the program to understand it clearly.

Example

import java.util.*; public class Main{ public static void main(String args[]) { int num; Scanner inn = new Scanner(System.in); mainLoop: while (true) { System.out.println("\n***Menu***"); System.out.println("1. Check if a Number is Positive or Negative"); System.out.println("2. Check Whether a Number is Even or Odd"); System.out.println("3. Terminate the program"); System.out.println("Enter action number (1-3): "); int command; if (inn.hasNextInt()){ command = inn.nextInt(); }else { System.out.println("\nILLEGAL RESPONSE. YOU MUST ENTER A NUMBER."); inn.nextLine(); continue; } switch(command) { case 1: System.out.print("Enter a number: "); num = inn.nextInt(); if(num>0) { System.out.println("Entered number "+num+" is positive."); } //checks the number is less than 0 or not else if(num<0) { System.out.println("Entered number "+num+" is negative."); } else { System.out.println("The number is zero."); } break; case 2: System.out.print("Enter a number: "); num = inn.nextInt(); if(num % 2 == 0) { System.out.println("Entered number "+num+" is even."); } else { System.out.println("Entered number "+num+" is odd."); } break; case 3: System.out.println("Program terminated"); break mainLoop; default: System.out.println("Wrong choice!!"); } } } }

Output

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
1
Enter a number: 45
Entered number 45 is positive.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
2
Enter a number: 45
Entered number 45 is odd.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
1
Enter a number: -456
Entered number -456 is negative.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
2
Enter a number: 2022
Entered number 2022 is even.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
x
ILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
ILLEGAL RESPONSE. YOU MUST ENTER A NUMBER.

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
5
Wrong choice!!

***Menu***
1. Check if a Number is Positive or Negative
2. Check Whether a Number is Even or Odd
3. Terminate the program
Enter action number (1-3):
3
Program terminated

In this article, we explored how to check if a number is positive, negative or even, odd in Java by using a menu driven approach.

Updated on: 27-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements