Java program to find if the given number is positive or negative

Java Programming Java8Java Technologies

Read a number from the user using the Scanner class's method. check whether the given number is greater, lesser or, equal to 0. If it is greater given number is positive if the lesser given number is negative. the else given number is neither positive or negative.

Example

import java.util.Scanner;

public class PositiveOrNegative {
   public static void main(String args[]){
      int num;
      System.out.println("Enter a number ::");
      Scanner sc = new Scanner(System.in);
      num = sc.nextInt();

      if (num > 0){
         System.out.println("Given number is a positive integer");
      } else if(num < 0){
         System.out.println("Given number is a negative integer");
      } else {
         System.out.println("Given number is neither positive nor negative integer");
      }
   }
}

Output 1

Enter a number ::
55
Given number is a positive integer

Output 2

Enter a number ::
-88
Given number is a negative integer
raja
Updated on 13-Mar-2020 07:03:14

Advertisements