Dremendo Tag Line

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

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 C 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 C to input an integer and check whether it is a positive number or negative number or a zero using the switch case statement.

Program

#include <stdio.h>
#include <conio.h>

int main()
{
    int n,c;
    printf("Enter a number ");
    scanf("%d",&n);
    c=(n>0)?1:(n<0)?2:3;

    switch(c)
    {
        case 1:
            printf("Positive Number");
            break;

        case 2:
            printf("Negative Number");
            break;

        default:
            printf("Zero");
    }
    return 0;
}

Output

Enter a number -14
Negative Number
video-poster