Java program to find Positive or Negative Number

Write a Java program to find a Positive or Negative Number or zero using the If Else Statement, Else If Statement, and Ternary Operator with an 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 the If Else

This program allows the user to enter any value. Next, this Java program finds or checks whether the entered number is either positive or negative using the 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 example, first, the if condition checks whether the given number is greater than or equal to 0.

  • If the condition fails in the 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 program checks or finds whether the entered number is either positive, negative, or zero using the Conditional Operator or 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");
	}
}
Java program to find Positive or Negative Number using Ternary Operator

Check for Negative Value

 Please Enter the any integer Value: 
-15

NEGATIVE

Let us look closer into the Java Conditional operator.

The first statement will execute if the condition (number >= 0) is true. If it fails, 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 Functions

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 into a separate method.

If you observe the last statement in this Positive or Negative Numbers program, we call the poOrNe method.

From the below positiveOrNegative method snippet, you can observe that this method accepts one integer type argument. 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");
		}
	}
}
Java program to find Positive or Negative Number using Functions

Java program to find Positive or Negative Number using OOPs

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

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 Java program to find the Positive or Negative Number, 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 the If statement to check whether the given number is greater than or equal to 0 or not. If it is True, we print a Positive Number statement. Otherwise, we print a Negative statement using the 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 example, we created an instance / created an Object of the PositiveOrNegative Class

PositiveOrNegative pn = new PositiveOrNegative();

Next, we assign 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);