
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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. The 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.
Problem Statement
Write a Java program to check positive negative or odd-even numbers ?
Input 1
Suppose the input number is 12.
Output 1
Checking it as a positive or negative number, it will result 12 as a positive number as it is greater than 0.
Input 2
Suppose the input number is -12.
Output 2
Checking it as a positive or negative number, it will result -12 as a negative number as it is less than 0.
Input 3
Suppose the input number is 2022.
Output 3
Checking it as an even or odd number, it will result 2022 as an even number as it is divisible by 2.
Input 4
Suppose the input number is 7.
Output 4
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 an 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 }
Steps to check positive negative or odd-even number
Following are the steps to positive negative or odd-even numbers ?
- Ask the user to input the desired number.
- Display the menu.
- Ask the user to enter their choice.
- Use a switch case to go to the choice and perform the operation.
- Print the result.
Java menu driven program to check positive negative or odd even number
Let's see the Java program to understand it clearly ?
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.