Number entered is positive, negative or zero using nested if-else
C program to check whether a number is positive, negative or zero using nested if-else.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("Enter a number\t: ");
scanf("%d",&num);
if(num >= 0)
{
if(num == 0)
printf("Number entered is zero");
else
printf("Number entered is positive");
}
else
printf("Number entered is negative");
getch();
}
Output
********** Run1 **********
Enter a number : 23
Number entered is positive
********** Run2 **********
Enter a number : -21
Number entered is negative
********** Run3 **********
Enter a number : 0
Number entered is zero
Explanation
A number is taken as an input from the user and stored in the variable named say 'num'
First of all, condition
If true, then the number is either zero or a positive value. So, checked for both the cases one by one using another if else construct.
If false, then the number is a negative number.
First of all, condition
(num >= 0)
is checked.
If true, then the number is either zero or a positive value. So, checked for both the cases one by one using another if else construct.
If false, then the number is a negative number.