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 the given number is positive. If the number is less than 0, the given number is negative.

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

This Java 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

// Java program to find Positive or Negative Number using 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

// Java program to find Positive or Negative Number using Ternary Operator
package SimpleNumberPrograms;

import java.util.Scanner;

public class PositiveOrNegativeUsingConditional {
	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();
		
		System.out.println((Number>=0)? "\n You have entered POSITIVE Number":
                                                "\n You have entered NEGATIVE Number");
	}
}
 Please Enter the any integer Value: 
215

 You have entered POSITIVE Number

Check for Negative Value

 Please Enter the any integer Value: 
-15

 You have entered NEGATIVE Number

Let us look closer into the Java Conditional operator.

System.out.println((Number>=0)? "\n You have entered POSITIVE Number":
                               "\n You have entered NEGATIVE Number");

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 You have entered POSITIVE Number
  • If this condition is false, then You have entered NEGATIVE Number 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.

package SimpleNumberPrograms;

import java.util.Scanner;

public class PositiveOrNegativeUsingMethods {
	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();
		
		positiveOrNegative(Number);

	}	
	public static void positiveOrNegative(int Number) {
		if (Number >= 0) {
			System.out.println(" You have entered POSITIVE Number");
		}
		else {
			System.out.println(" You have entered NEGATIVE Number");
		}
	}
}

 Please Enter the any integer Value: 
56
 You have entered POSITIVE Number

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

positiveOrNegative(Number);

Let us see the code inside the positiveOrNegative method

public static void positiveOrNegative(int Number) {
	if (Number >= 0) {
		System.out.println(" You have entered POSITIVE Number");
	}
	else {
		System.out.println(" You have entered NEGATIVE Number");
	}
}

From the above code 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.

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 positiveOrNegative() {
		if (x >= 0) {
			System.out.println("\n First Method: You have entered POSITIVE Number");
		}
		else {
			System.out.println("\n First Method: You have entered NEGATIVE Number");
		}
	}
	
	public void positiveOrNegativeAgain(int Number) {
		if (Number >= 0) {
			System.out.println("\n Second Method: You have entered POSITIVE Number");
		}
		else {
			System.out.println("\n Second Method: You have entered NEGATIVE Number");
		}
	}

}

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 PositiveOrNegativeUsingClass {
	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.positiveOrNegative();
		pn.positiveOrNegativeAgain(Number);
	}
}
 Please Enter the any integer Value: 
55
 First Method: You have entered POSITIVE Number
 Second Method: You have entered POSITIVE Number

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 Number 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 PositiveOrNegative and PositiveOrNegativeAgain methods.

pn.positiveOrNegative();
pn.positiveOrNegativeAgain(Number);