Dremendo Tag Line

Check number is positive, negative or zero in Java

if else if - Question 2

In this question, we will see how to input a number and check if it is a positive number or a negative number or a zero in Java programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.

Q2) Write a program in Java to input a number and check if it is a positive number or a negative number or a zero.

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 if(n<0)
        {
            System.out.print("Negative number");
        }
        else
        {
            System.out.print("Zero");
        }
    }
}

Output

Enter a number -5
Negative number
video-poster