Java Program to Check Whether a Number is Positive or Negative


Whether the specified number is positive or negative can be defined 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. To check whether a given number is positive or negative in Java, we can use Java conditional statements like if-else blocks or ternary operators. In this article, we are going to explore the mentioned ways to identify if a number is positive or negative with the help of Java programs.

Java Program to Check Whether a Number is Positive or Negative

In this section, we will write some Java programs to check whether the given number is positive or negative. Before that let's discuss the problem statement with the help of an example −

Instance

Input 1

Enter the number: -3

Output

-30 is a negative number

Input 2

Enter the number: 35

Output

35 is a positive number

We are going to use the following ways to check positive and negative numbers −

  • if else if conditional block

  • Ternary operator

Using if else if block

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

Example 1

In this Java program, we will take a number as input from user with the help of Scanner class and check whether that number is positive or negative using the if else if block.

import java.util.Scanner;
public class Example1 {
   public static void main(String[] args) {
      int myInput;
      // creating an instance of Scanner class
      Scanner my_scanner = new Scanner(System.in);
      System.out.print("Enter the number : ");
      // to take input from user
      myInput = my_scanner.nextInt();
      // 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 1

Enter the number : 45
45 is a positive number

Output 2

Enter the number : -35
-35 is a negative number

Example 2

In the following example, instead of taking input from the user we will declare and initialize an integer variable to check if it is a positive number 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
      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

Use of 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, used to decide which value should be assigned to the specified variable. By using the ternary operator we can convert our lengthy code into a few liner code.

Example 3

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

public class Example3 {
   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 three different Java program 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: 10-Aug-2023

433 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements