Programming Examples
Java program to check number is negative positive or zero
Write a Program input any Integer number and check number is Negative, Positive or Zero. use only if statement.
Solution
import java.util.Scanner;
public class OnlyIfStatement
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (number > 0) {
System.out.println("The number is positive.");
}
if (number < 0) {
System.out.println("The number is negative.");
}
if (number == 0) {
System.out.println("The number is zero.");
}
}
}
Output
Enter a number: 5
The number is positive.
----------------------------------
Enter a number: -3
The number is negative.
----------------------------------------
Enter a number: 0
The number is zero.