Check a number is positive or not in Java
if else - Question 2
In this question, we will see how to check if a number is a positive number or not in Java programming using the if else statement. To know more about if else statement click on the if else statement lesson.
Q2) Write a program in Java to input a number and check if it is a positive number or not. A positive number is a number which is greater than 0.
Program
import java.util.Scanner;
public class Q2
{
public static void main(String args[])
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number ");
n=sc.nextInt();
if(n>0)
{
System.out.print("Positive Number");
}
else
{
System.out.print("Not Positive Number");
}
}
}
Output
Enter a number 5 Positive Number