Java program to find Positive or Negative Number

Write Java program to find Positive or Negative Number or zero using If Else Statement, Else If Statement, and Ternary Operator with example. If the number is greater than 0, then it is positive. If the number is less than 0, it is negative.

Java program to find Positive or Negative Number using If Else Statement

This program allows the user to enter any number. Next, it checks whether the entered value is either positive or negative using Java If Else Statement

package SimpleNumberPrograms;

import java.util.Scanner;

public class PositiveOrNegativeUsingIf {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter the any integer Value: ");
		Number = sc.nextInt();
		
		if (Number >= 0) {
			System.out.println("\n You have entered POSITIVE Number");
		}
		else {
			System.out.println("\n You have entered NEGATIVE Number");
		}
	}
}

From the below Java screenshot, you can observe that we entered the Number value as 10. It means, the condition is TRUE, that is why it is displaying the statement inside the If block.

Java program to find Positive or Negative Number using If else 1

Check for Negative Value

 Please Enter the any integer Value: 
-5

 You have entered NEGATIVE Number

Within this Java program to find Positive or Negative Number example, first, if condition checks whether the given number is greater than or equal to 0.

  • If the condition fails in Java If Else Statement, then the given number will be negative.
  • If the condition is true, then the given number is a positive integer.

Java program to find Positive or Negative Number using Ternary Operator

This Java program checks whether the entered value is either positive or negative or zero using Conditional Operator or Java Ternary Operator


import java.util.Scanner;

public class PositiveOrNegativeUsingConditional {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Num;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter the any integer Value: ");
		Num = sc.nextInt();
		
		System.out.println((Num. >=0)? "\nPOSITIVE":
                                                "\nNEGATIVE");
	}
}
 Please Enter the any integer Value: 
215

POSITIVE

Check for Negative Value

 Please Enter the any integer Value: 
-15

NEGATIVE

Let us look closer into the Java Conditional operator.

If the condition (number>= 0) is true then first statement will execute. If it fails, then it will check the second condition, which is after the : symbol (number< 0).

  • When the condition is true, then it will print the first message POSITIVE
  • If this condition is false, then NEGATIVE will be printed

Java program to find Positive or Negative Number using Methods

In this Java program, we are using the same steps that we followed in our first example. However, we separated the positive or negative number logic and placed it in a separate method.

If you observe the last statement in this Java program to find Positive or Negative Numbers, we are calling the poOrNe method.

From the below positiveOrNegative method snippet, you can observe that this method accepts one argument of integer type. Within the function, we are performing the same operation that we explained in our previous example.

package SimpleNumberPrograms;

import java.util.Scanner;

public class PosOrNegUsingMethods {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Num;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter the any integer Value: ");
		Num = sc.nextInt();
		
		poOrNe(Num);

	}	
	public static void poOrNe(int Num) {
		if (Num >= 0) {
			System.out.println("POSITIVE");
		}
		else {
			System.out.println("NEGATIVE");
		}
	}
}

 Please Enter the any integer Value: 
56
POSITIVE

Java program to find Positive or Negative Number using OOPs

In this Java program, we are dividing the code using the Object-Oriented Programming. To do this, First, we will create a class that holds two methods.

TIP: In general, You do not have to write the first method. We used this method to show the available options

package SimpleNumberPrograms;

public class PositiveOrNegative {
	int x;
	
	public void Sample() {
		if (x >= 0) {
			System.out.println("\n First Method: You have entered POSITIVE");
		}
		else {
			System.out.println("\n First Method: You have entered NEGATIVE");
		}
	}
	
	public void SampleAgain(int Number) {
		if (Number >= 0) {
			System.out.println("\n Second Method: You have entered POSITIVE");
		}
		else {
			System.out.println("\n Second Method: You have entered NEGATIVE");
		}
	}

}

Within the Main program to find Positive or Negative Numbers, we will create an instance of the above-specified class and call the methods.

package SimpleNumberPrograms;

import java.util.Scanner;

public class PoOrNeUsingClass {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Number;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter the First integer Value: ");
		Number = sc.nextInt();
		
		PositiveOrNegative pn = new PositiveOrNegative();
		pn.x = Number;
		
		pn.Sample();
		pn.SampleAgain(Number);
	}
}
 Please Enter the any integer Value: 
55
 First Method: You have entered POSITIVE
 Second Method: You have entered POSITIVE

PositiveOrNegative Class Analysis:

  • First we declared a function positiveOrNegative with zero arguments. Within the function, we used If statement to check whether the given number is greater than or equal to 0 or not. If it is True, we are printing Positive Number statement otherwise, Negative statement using System.out.println statement.
  • Next, we declared a function positiveOrNegativeAgain with one argument. Within the function, we performed the same operation that we explained in our first function.

Main Class Analysis:

In this Java program to find Positive or Negative Numbers example, we created an instance / created an Object of the PositiveOrNegative Class

PositiveOrNegative pn = new PositiveOrNegative();

Next, we are assigning the user entered values to the PositiveOrNegative Class variables.

pn.x = Number;

Next, we are calling the Sample and SampleAgain methods.

pn.Sample();
pn.SampleAgain(Number);