Home » Programming » C Examples » C program to Check whether a number is positive, negative or zero

# C program to Check whether a number is positive, negative or zero

In this post, we will learn how to check whether a number is positive or negative or zero in C programming language.

A number is positive if it is greater than zero. Similarly, a number is negative if it is smaller than zero.

## Algorithm to check whether a number is positive, negative or zero

We will use a variable “num” to store the value of a number to check. Algorithm is given as follows,

```Step 1: Start.
Step 2: Take a number as input in the variable “num”.
Step 3: Check if the num is greater than 0. If yes, print “Entered number is positive”.
Step 4: Check if the num is less than 0. If yes, print “Entered number is negative”.
Step 5: Else print “Entered number is zero”.
Step 6: End.```

In short,

```Start,
If num > 0 then
“Entered number is positive”
else if num < 0 then
“Entered number is negative”
else
“Entered number is zero”
End.```

## Program to Check whether a number is positive, negative or zero using if

```#include <stdio.h>
int main() {
float num;
printf("Enter a number to check: ");
scanf("%f", &num);

/* Check if the number is positive, negative or zero */
if (num > 0) {
printf("Entered number is Positive.");
}
if (num < 0) {
printf("Entered number is negative.");
}
if (num == 0) {
printf("Entered number is 0.");
}
return 0;
}```

Output

```Enter a number to check: 200
Entered number is Positive.```

Explanation

• In the first place, we have to take input in a variable. Here we are using variable num of data type float.
```float num;
printf("Enter a number to check: ");
scanf("%f", &num);```
• After that, we will check if the number is positive or not.
```if (num > 0) {
printf("Entered number is Positive.");
}```

If number is greater than 0 then number is positive. If yes we will print saying the number is positive.

• Then, even if the entered number is positive we have to check if the number is negative or not. This is the drawback of using if multiple times rather than if…else or if…else…if.
```if (num < 0) {
printf("Entered number is negative.");
}```

If the number is less than 0 then it is negative.

• Again we will have to check if the number is 0 not. This step is also executed even if the number is found to be negative or positive in previous steps.
```if (num == 0) {
printf("Entered number is 0");
}```

If the number is equal to 0 then it is zero.

## Using if…else

Since we are comparing the same number multiple times even if we get our answer in the first “if” statement this method is inefficient. We can use if…else to eliminate this problem.

```#include <stdio.h>

int main() {
float num;
printf("Enter a number to check: ");
scanf("%f", &num);

/* Check if the number is positive, negative or zero */
if (num > 0) {
printf("Entered number is Positive.");
} else {
if (num < 0)
printf("Entered number is negative.");
else
printf("Entered number is 0.");
}
return 0;
}```

Output

```Enter a number to check: -200
Entered number is negative.  ```

Explanation

• First, we will check if the number is positive we will print specifying the number is positive and control is transferred to the immediate statement after if…else i.e. return 0; in this case.
```if (num > 0) {
printf("Entered number is Positive.");
} else {
. . .
}```
• If number is not positive control is transferred to else block. In else block, we will check if the number is negative or not. If the number is negative we will print specifying that the number is negative or the control is passed to next else block.
```else {
if (num < 0)
printf("Entered number is negative.");
else
. . .
}```
• Finally, we are in last else block. If control is in this place that means the number itself is zero we don’t have to check any further. This is because the number is already checked for positive or negative and if the number is not both of them it is zero.
```else
printf("Entered number is 0.");```

## Using else…if

However, using if…else provides us better efficiency but we are nesting the if…else statements which makes our code harder to read. To tackle this problem we can use else…if.

```#include <stdio.h>

int main() {
float num;
printf("Enter a number to check: ");
scanf("%f", &num);

/* Check if the number is positive, negative or zero */
if (num > 0) {
printf("Entered number is Positive.");
} else if (num < 0) {
printf("Entered number is negative.");
} else {
printf("Entered number is 0.");
}
return 0;
}```

Output

```Enter a number to check: 0
Entered number is 0.```

Explanation

• As we have done previously, we will check whether the number is positive or not. This step is same as we have done.

if (num > 0) {
printf(“Entered number is Positive.”);
}

If number is positive, we will print that number is positive and the control is transferred to the next block after the if…else…if i.e. return 0; in this case. If not, control is transferred to else if.

• else if work same as the if blocks. We can check for any condition in this block. Here, we are checking if the number is negative or not.
```else if (num < 0) {
printf("Entered number is negative.");
}```

If the number is negative we will print that the number is negative and the control is transferred to the next block after the if…else…if i.e. return 0; same as if block. If not, control is transferred to else block.

• At last, in the else block we will print specifying number is zero since both condition for negative and positive number is already checked.
```else {
printf("Entered number is 0.");
}```

More Examples