In this article, we will write a C program to check whether a number is positive, negative or zero.
The program takes a number as input from the user, checks whether it is positive, negative or zero, and prints the result on the screen.
Sample Input:
Enter a number: -25
Output:
The number is negative.
A number is called a negative number if it is less than 0 such as -10, -20, -30, etc and positive if it is greater than 0 such as 10, 20, 30, etc.
To check if the number is negative or positive, we can use the less than(<
) and greater than(>
) operators with an if...else
block.
See implementation in the below program:
// C program to check whether a number is positive, negative or zero #include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); if (number < 0) { printf("The number is negative."); } else if (number > 0) { printf("The number is positive."); } else { printf("The number is zero."); } return 0; }
Output:
Enter a number: -25 The number is negative.
code Explanation:
- The program takes a number from the user as input and stores it in the
number
variable. - To check if the number is negative, positive or zero, we have used an
if...else
block. - If the number is less than zero, the code inside the
if block
is executed. If not, the program moves next and checks if the number is greater than zero using anelse...if
statement, if it is, the code inside theelse...if
block is executed. - If the above two cases don’t match, it means the entered number is zero, therefore, the
else
block’s code is executed. - Finally, the output is printed on the screen using the printf() function.
Example 2: Use a Switch Case Statement to Check if the Number is Positive or Negative
We can also use a switch case statement to check if the number is positive or negative.
The switch case statement has the following syntax:
switch(expression) { case constant1: // code to be executed if expression is equal to constant1; break; case constant2: // code to be executed if expression is equal to constant2; break; // more cases can be added here default: // code to be executed if expression doesn't match any of the constants; }
The switch case expects an expression which on execution returns a fixed list of values. These values can be a number, string, boolean, etc. based on the expression.
To check if the number is positive, negative or zero using a switch case statement, we can use the following algorithm:
- Get the number from the user and store it in some variable, say
number
. - Inside the expression of the switch case, we can put a condition
switch(number < 0)
. This expression will return 1, if the number is negative, otherwise, it will return 0. - If the number is negative,
case 1
will run and print that the number is negative. - If the number is not negative, the
case 0
will run, where we can again put a nested switch case. - The expression of the nested switch statement will be
switch(number > 0)
. This expression will return 1 if the number is positive, and 0 if the number is zero. - Here,
case 1
will run if the number is greater than 0 andcase 0
will run if the number is 0.
See implementation in the following program:
// C program to check whether a number is positive, negative or zero #include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); switch (number < 0) { // Number is negative case 1: printf("Number is negative"); break; // Number is either positive or zero case 0: switch (number > 0) { // Number is positive case 1: printf("Number is positive"); break; // Number is zero case 0: printf("Number is zero."); break; } } return 0; }
Output:
Enter a number: -90 Number is negative
Thanks for reading.