Write a C program to check whether a number is positive, negative or zero
Description: Take a number as input. Print if it is positive, negative or zero.
Hint:
- Compare the number with 0 using
>and<.
C program to check positive, negative or zero
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0) {
printf("Positive number\n");
} else if (n < 0) {
printf("Negative number\n");
} else {
printf("Zero\n");
}
return 0;
}
Output:
Output of the above program is
Enter a number: -8
Negative number
How it works
- The first condition
n > 0is false for -8, the secondn < 0is true, so “Negative number” is printed.
Read Else If Ladder.
Learn more about the similar topics:
| Tutorials |
|---|
| C If Else Statement |
| C Else If Ladder and Nested If |
| C Switch Statement |
| C While Loop |
| C Do While Loop |
| C For Loop |
| C Nested Loops |