Codeforcoding

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

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

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)

 

 

Java program to check whether a number is Positive or Negative or Zero
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

C program to check whether a number is prime or not

C++ program to check whether a number is prime or not

Python  program to check whether a number is prime or not

Java program to check whether a number is prime or not

 

Suggested for you

Operator in Java language

Data type and variable in Java language

If statement in Java language

Nested if statements in Java language

 

Python program to find sum of two numbers without using arithmetic operators
C program to check whether a number is Positive or Negative or Zero
Exit mobile version