- Given a integer number, check given number is positive. negative or zero.
- If the input number is greater than zero then it’s positive number
- or If input number is less zero then it’s termed as negative number
- or if input number is neither positive nor negative then it’s zero
Examples to check integer number is positive, negative or zero
Example 1 : Check input number 27 is positive, negative or zero
As input number 27 is greater than so it's positive number
Output of program would be "Number is positive"
Example 2 : Check input number -9 is positive, negative or zero
As input number is less than zero 0 so it's negative number
Output of program would be "Number is Negative"
Program: Check input number is positive negative or zero in Java
package org.learn.beginner;
import java.util.Scanner;
public class PositiveNegativeDemo {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter input number: ");
int inputNumber = scanner.nextInt();
if (inputNumber < 0) {
System.out.printf("Input number %d is a negative number", inputNumber);
} else if (inputNumber > 0) {
System.out.printf("Input number %d is a positive number", inputNumber);
} else {
System.out.println("Input number is zero");
}
}
}
}
Output: positive negative or zero number in java
Enter input number: 27
Input number 27 is a positive number
************************************************
Enter input number: -9
Input number -9 is a negative number
************************************************
Enter input number: 0
Input number is zero