
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Program to check if a number is Positive, Negative, Odd, Even, Zero?
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- JAVA Menu Driven Program to Check Character is String, Number or Special Character
- Java Menu Driven Program to Check Type of a Number
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Number is Even or Odd
- C++ Program to Check Whether Number is Even or Odd
- Haskell Program to Check Whether a Number is Positive or Negative
- C++ Program to Check Whether a Number is Positive or Negative
- Haskell Program to Check Whether a Number is Even or Odd
- C program to Check Whether a Number is Positive or Negative or Zero?
- Python Program to Check if a Number is Positive, Negative or 0
- 8085 program to check whether the given number is even or odd
- Java program to find whether given number is even or odd
- Java Program to check if count of divisors is even or odd
