In this tutorial, we will write a program to check whether the number is positive or negative in java. The program takes the user input for the number to be checked and performs the calculation.
Before, we begin you need to have the idea of the proper working of if-else..if statement. Click the link below.:
Java Program to Check for Positive and Negative Numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //check for positive or negative of a number in java import java.util.Scanner; public class PositiveNegativeNumber { public static void main(String[] args) { int num; Scanner s = new Scanner(System.in); System.out.print("Enter the number: "); num = s.nextInt(); if(num > 0) { System.out.println(num+" is a positive number"); } else if(num < 0) { System.out.println(num+" is a negative number"); } else { System.out.println(num+" is neither positive nor negative"); } } } |
Output:
Enter the number: -5
-5 is a negative number