Java program to take a number as input and tells its positive or negative
Following program shows you how to take a number as input and tells its positive or negative.
In this program we get number from user, and if the number is less than zero it prints negative, and if the number is grater than zero it prints positive
import java.util.Scanner;
public class BasicMath7 {
public static void main(String[] args) {
System.out.println("Please enter a number:");
Scanner in = new Scanner(System.in);
int i = in.nextInt();
if (i < 0) {
System.out.println("Given number is -ve");
} else {
System.out.println("Given number is +ve");
}
}
}
Output:
Example1:
Please enter a number:
2
Given number is +ve
Example2:
Please enter a number:
-3
Given number is -ve