In this tutorial you will be learning Writing Java Program to check a Given input number is a positive number or a negative number.
For Example
1 : Suppose given input = 10
Output = Given number is positive
The above number is positive because it is greater than zero.
2: Suppose given input = -2
Output = Given number is negative
The above number is negative because it is smaller than zero.
Java Program to check input number is positive or negative
import java.util.*;
public class Main
{
public static void main (String[]args)
{
System.out.println("Check Input number is Positive or Negative");
Scanner sc = new Scanner (System.in);
System.out.println ("Enter a input");
int num = sc.nextInt();;
if (num>0) {
System.out.println("Given input is Positive Number.");
}else if(num<0)
{
System.out.println("Given input is Negative Number.");
}else{
System.out.println("Given Input is zero.");
}
}
}
Output 1:
Check Input number is Positive or Negative
Enter a input
-4
Given input is Negative Number.
Output 2:
Check Input number is Positive or Negative
Enter a input
22
Given input is Positive Number.