In this program, we have to take an integer as input from the user using scanf()
function and store it in a variable. Then compare the number with 0.
If num>0
: Number is Positive
If num<0
: Number is Negative
Else: Number is Zero
Using if else condition
C
#include <stdio.h>
void main(){
int num;
printf("Enter a num: ");
scanf("%d", &num);
if (num > 0){
printf("Number is positive");
}
else if (num < 0){
printf("Number is negative");
}
else{
printf("Number is Zero");
}
}
Using Ternary Operator
C
#include <stdio.h>
void main(){
int num;
printf("Enter a num: ");
scanf("%d", &num);
num > 0 ?
printf("Number is positive") :
num < 0 ?
printf("Number is Negative") :
printf("Number is Zero");
}
Output
Enter a num: 55
Number is positive