Positive Negative Zero
Table of Contents
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
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,
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
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
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
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
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
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.