Write a Java program to check whether a number is positive, negative or zero using switch case

In this post, we will write two java programs, first java programs checks whether the specified number is positive or negative. The second program takes the input number (entered by user) and checks whether it is positive or negative and displays the result. → If a number is greater than zero then it is a positive number → If a number is less than zero then it is a negative number

→ If a number is equal to zero then it is neither negative nor positive.

In this tutorial, we will discuss a concept in Java program to check whether a number is Positive or Negative or Zero

In this post, we will learn how to check if the number is positive or negative or zero in Java programming language

Logic of the program

If the number is greater than zero it is a positive number  – (if the number>=0)

If the number is less than to zero it is a Negative number – (if the number<=0)

if the number is equal to zero the number is zero – (if the number==0)

Write a Java program to check whether a number is positive, negative or zero using switch case
Java program to number is Positive or Negative or Zero

Using if else if statements

Program 1

This program allows the user to enter a number and check if the number is positive or negative or zero using if else if statements

import java.util.Scanner; class CheckPosNegZero{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number: "); int num1=scan.nextInt();//get input from the user for num1 if(num1>0){ System.out.println(num1+" is a positive number"); } else if(num1<0){ System.out.println(num1+" is a Negative number"); } else{ System.out.println("number is Zero"); } } }

When the above code is executed, it produces the following result

case 1

Enter the number: 4 4 is a positive number

case 2

Enter the number: -6 -6 is a Negative number

case 3

Enter the number: 0 Number is zero

Using nested  if statements

Program 2

This program allows the user to enter a number and check if the number is positive or negative or zero using nested if statements

import java.util.Scanner; class CheckPosNegZero1{ public static void main(String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number: "); int num1=scan.nextInt();//get input from the user for num1 if(num1<=0){ if(num1==0){ System.out.println(" you entered zero"); } else{ System.out.println(num1+" is a Negative number"); } } else{ System.out.println(num1+" is a positive number"); } } }

When the above code is executed, it produces the following result

case 1

Enter the number: 20 20 is a positive number

case 2

Enter the number: -15 -15 is a Negative number

case 3

Enter the number: 0 you entered zero

Similar post

C program to check whether a number is Positive or Negative or Zero

C++ program to check whether a number is Positive or Negative or Zero

Python  program to check whether a number is Positive or Negative or Zero

Suggested for you

Operator in java

Data type and variable in Java

If statement in Java

Nested if statements in Java

Last update on May 16 2022 12:15:45 (UTC/GMT +8 hours)

Write a Java program that reads an integer and check whether it is negative, zero, or positive.

Test Data
Input a number: 7

Pictorial Presentation:

Write a Java program to check whether a number is positive, negative or zero using switch case

Sample Solution:

Java Code:

import java.util.Scanner; public class Exercise27 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input a number: "); int n = in.nextInt(); if (n > 0) { System.out.println("Number is positive"); } else if (n < 0) { System.out.println("Number is negative"); } else { System.out.println("Number is zero"); } } }

Sample Output:

Input a number: 7 Number is positive

Flowchart:

Write a Java program to check whether a number is positive, negative or zero using switch case

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to display the following character rhombus structure.
Next: Write a Java program that reads a floating-point number. If the number is zero it prints "zero", otherwise, print "positive" or "negative". Add "small" if the absolute value of the number is less than 1, or "large" if it exceeds 1,000,000.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Reverse String:

public static String reverseString(String input) { return new StringBuilder(input).reverse().toString(); }

Ref: https://bit.ly/3tvPp1Y

In java programming, if else statements can be used to do the different logic's based on condition.

This java program is used to check whether number is positive number or negative number using if else statements.

import java.util.Scanner; public class Positive{ public static boolean positive(int number){ if(number >= 0){ return true; } else { return false; } } public static void main(String[] args){ System.out.print("Enter Number: "); Scanner sc=new Scanner(System.in); int number = sc.nextInt(); if(positive(number)){ System.out.println("Number is positive!"); } else { System.out.println("Number is negative!"); } } } Output: D:\Java_Programs>javac Positive.java D:\Java_Programs>java Positive Enter Number: 5 Number is positive! D:\Java_Programs>javac Positive.java D:\Java_Programs>java Positive Enter Number: -54 Number is negative! D:\Java_Programs>javac Positive.java D:\Java_Programs>java Positive Enter Number: 0 Number is positive!

Java Programming Checks Number is Positive or Negative using switch case and static method

This java program is used to identify positive or negative and uses different logic's for positive and negative using switch case statements.

Used indefinite loop to check the user input number is positive or negative, user has to specify n or no to exit the program.

import java.util.Scanner; public class Positive{ // Checks positive or negative public static int positive(int number){ if(number > 0){ return 1; } else if (number < 0){ return 0; } else { return -1; } } public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(true){ System.out.print("Enter Number: "); int number = sc.nextInt(); int caseID = positive(number); // different logics for positive, negative or neither positive nor negative. switch(caseID){ case 0: System.out.println("Number is negative!"); break; case 1: System.out.println("Number is positive!"); break; default: System.out.println("Number is neither positve or positive!"); } System.out.print("Do you want to check for other number (Y->yes / N ->No): "); String ch = sc.next(); if(ch.toLowerCase() == "N" || ch.toLowerCase() == "no"){ break; } } } } Output: D:\Java_Programs>javac Positive.java D:\Java_Programs>java Positive Enter Number: 5 Number is positive! Do you want to check for other number (Y->yes / N ->No): y Enter Number: 0 Number is neither positve or positive! Do you want to check for other number (Y->yes / N ->No): Y Enter Number: -6 Number is negative! Do you want to check for other number (Y->yes / N ->No): no

Java Programming Checks Number is Positive or Negative using ternary operation

import java.util.Scanner; public class Positive{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(true){ System.out.print("Enter Number: "); int number = sc.nextInt(); String msg = (number >= 0)?"Number is positive!":"Number is negative!"; System.out.println(msg); System.out.print("Do you want to check for other number (Y->yes / N ->No): "); String ch = sc.next(); if(ch.toLowerCase().equals("n") || ch.toLowerCase().equals("no")){ break; } } } } Output: D:\Java_Programs>javac Positive.java D:\Java_Programs>java Positive Enter Number: 5 Number is positive! Do you want to check for other number (Y->yes / N ->No): y Enter Number: -8 Number is negative! Do you want to check for other number (Y->yes / N ->No): n

Java Programming Language Blog