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 <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n,c;
cout<<"Enter a number ";
cin>>n;
c=(n>0)?1:(n<0)?2:3;
switch(c)
{
case 1:
cout<<"Positive Number";
break;
case 2:
cout<<"Negative Number";
break;
default:
cout<<"Zero";
}
return 0;
}
Output
Enter a number -14 Negative Number