Java program to check whether a number is positive or negative


In this article, we will learn to check whether the number is positive or negative in Java. To check whether the specified number is positive or negative can be determined with respect to 0. A number greater than 0 is considered a positive number whereas a number less than 0 is considered a negative number, we can use Java conditional statements like if-else-if blocks or ternary operators. Before that let's discuss the problem statement with the help of examples −

Problem Statement

Given an integer number as an input, write a Java program to check whether the number is positive or negative.

Input 1

num = -30

Output 1

res = -30 is a negative number

Input 2

num = 35

Output 2

res = 35 is a positive number

Different Approaches

Following are the different approaches to check whether the number is positive or negative.

Using if else if block

In this approach, we will use the if-else-if block that allows us to pass multiple conditions and executes only that statement which is true.

Example

In the following Java program, we will use if-else block to check if the given integer variable is a positive number or negative.

public class Example1 {
   public static void main(String[] args) {
      int myInput = 788;
      System.out.println("The given number is: " + myInput);
      // to check given number is positive or negative
      if(myInput > 0) {
         System.out.println(myInput + " is a positive number");
      } else if(myInput == 0) {
         System.out.println(myInput + " is equal to zero");
      } else {
         System.out.println(myInput + " is a negative number");
      }
   }
}

Output

The given number is: 788
788 is a positive number

Using Ternary Operator

In Java, the ternary operator can be used as an alternative to the if-else condition in some situations. The ternary operator often known as a conditional operator consists of three operands. By using it we can determine if the specified variable is positive or negative.

Example

The following Java program demonstrates the practical implementation of the ternary operator in checking whether a number is positive or negative.

public class Example2 {
   public static void main(String[] args) {
      int myInput = 788;
      System.out.println("The given number is: " + myInput);
      // to check given number is positive or negative
      boolean isGreater = (myInput > 0) ? true : false;
      if(isGreater) {
         System.out.println(myInput + " is a positive number");
      } else {
         System.out.println(myInput + " is a negative number");
      }
   }
}

Output

The given number is: 788
788 is a positive number

Conclusion

In this article, we have learned about positive and negative numbers and also, how to identify if a given number is positive or not. To perform this operation, we have written two different Java programs which uses the if else if block and ternary operator. The ternary operator is the most efficient alternative to the if else if conditional block.

Updated on: 13-Sep-2024

841 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements