java

Java code to check if the given number is positive or negative or 0

Java code to check if the given number is positive or negative or 0

In this tutorial, we will discuss the  Java code to check if the given number is positive or negative or 0

In this post, we are going to learn how to check whether the given number is positive or Negative or zero  using 6 ways

 

The logic to check positive, negative or zero

  • Check the condition if(num<0), then number is negative
  • Check the condition if(num>0), then number is positive
  • Check the condition if(num==0), then number is zero

Program to Check if the given number is positive or negative or 0

Program to check if the given number is positive or negative or 0 – standard method

Program 1

//Java program to check whether the given number positive or negative or zero
class CheckPositiveNegative0{
public static void main(String args[]){
   int num=456;

  if(num>0){
  System.out.println(num+" is a positive number");
}else if(num<0){

  System.out.println(num+" is a Negative number");
}
else{
  System.out.println("The given number is zero");
}
}
}

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

456 is a positive number

 

In this program,

  • integer variable num  is declared and initialized
  • The given numbers are tested whether it is positive, negative or zero using if- else statements
  • Then,the program is displayed the output using System.out.print() function.

Program to check if the given number is positive or negative or 0 – Entered by user

Program 2

import java.util.Scanner;
class CheckPositiveNegativezero{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num
if(num>0){
  System.out.println(num+" is a positive number");
}else if(num<0){

  System.out.println(num+" is a Negative number");
}
else{
  System.out.println("The given number is zero");
}
}
}

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

Case 1

Enter the integer number:125
125 is a positive number

Case 2

Enter the integer number:-453
-453 is a negative number

Case 3

Enter the integer number:0
The given number is zero

 

Approach

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using if- else statements
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 – using Nested if

Program 3

import java.util.Scanner;
class CheckPositiveNegativeNestedif{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num
if(num<=0){
if(num==0){
  System.out.println("you entered zero");
}else{

  System.out.println(num+" is a Negative number");
}
}
else{
  System.out.println(num+" is a Positive number");
}
}
}

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

Case 1

Enter the integer number:25
25 is a positive number

Case 2

Enter the integer number:-43
-43 is a negative number

Case 3

Enter the integer number:0
you entered zero

 

Approach

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using Nested if- else statements
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 – using method

Program 4

import java.util.Scanner;
class CheckPositiveNegativeMethod{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num

PosNegZero(num);//method call

}
public static void PosNegZero(int num){//method definition
if(num>0){
  System.out.println(num+" is a positive number");
}else if(num<0){

  System.out.println(num+" is a Negative number");
}
else{
  System.out.println("The given number is zero");
}
}
}

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

Case 1

Enter the integer number:234
234 is a positive number

Case 2

Enter the integer number:-543
-543 is a negative number

Case 3

Enter the integer number:0
The given number is zero

Approach

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using user defined method
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 – using ternary operator

Program 5

import java.util.Scanner;
class CheckPositiveNegativeternary{
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");

int num=scan.nextInt();//get input from the user for num1

PosNegZero(num);//method call

}
public static void PosNegZero(int num){//method definition
System.out.println((num>=0)? "\n you have entered positive number" :
"\n you have entered Negative number");
}
}

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

Case 1

Enter the integer number:657
you have entered positive number

Case 2

Enter the integer number:-987
you have entered Negative number

Approach

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num
  • The given number is tested whether it is positive, negative or zero using ternary operator
  • Then,the program is displayed the output of using System.out.print() function,

 

Program to check if the given number is positive or negative or 0 -using switch case

Program 6

import java.util.Scanner;
class CheckPositiveNegativeSwitch1{
  public static int positive(int num){
    if(num>0){
      return 1;
    }
    else if(num<0){
      return 0;
    }
    else{
      return -1;
  }
  }
public static void main(String args[]){
    Scanner scan=new Scanner(System.in); //create a scanner object for input
  System.out.print("\nEnter the integer number: ");

int num=scan.nextInt();//get input from the user for num
int result=positive(num);

switch(result){
   case 0://check  num is negative
       System.out.print(num+" is negative");
    break;
  case 1://check  num is positive
       System.out.print(num+" is positive");
    break;
  default:
       System.out.print("the given number is zero");
    break;
}
}
}

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

Case 1

Enter the integer number:
567
567 is positive

Case 2

Enter the integer number:
-876
-876 is negative

Case 3

Enter the integer number:
0
The given number is zero

 

Suggested post

If statement in Java

Nested if in Java

Method in Java

switch case in Java

Operator in Java

Data type and variables in Java

 

Similar post

C program to find if the given number is positive or negative or 0

C++ program to find if the given number is positive or negative or 0

Python program to find if the given number is positive or negative or 0

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.