Dremendo Tag Line

Input integer check positive, negative or zero using switch case in Java

switch case - Question 4

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

Q4) Write a program in Java to input an integer and check whether it is a positive number or negative number or a zero using the switch case statement.

Program

import java.util.Scanner;

public class Q4
{
    public static void main(String args[])
    {
        int n,c;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number ");
        n=sc.nextInt();
        c=(n>0)?1:(n<0)?2:3;

        switch(c)
        {
            case 1:
                System.out.print("Positive Number");
                break;

            case 2:
                System.out.print("Negative Number");
                break;

            default:
                System.out.print("Zero");
        }
    }
}

Output

Enter a number -14
Negative Number
video-poster