(C programming Example for Beginners)
C Program to Check Whether a Number is Positive or Negative
In this example, you will learn to check whether a number (entered by the user) is negative or positive.
This program takes a number from the user and checks whether that number is either positive
or negative
or zero
.
Check Positive or Negative Using if…else
#include <stdio.h>
int main(){
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
} else
printf("You entered a positive number.");
return 0;
}
You can also solve this problem using nested if else
statement.
Check Positive or Negative Using Nested if…else
#include <stdio.h>
int main(){
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num < 0.0)
printf("You entered a negative number.");
else if (num > 0.0)
printf("You entered a positive number.");
else
printf("You entered 0.");
return 0;
}
Output 1
Enter a number: 12.3 You entered a positive number.
Output 2
Enter a number: 0 You entered 0.
C Example for Beginners: C Program to Check Whether a Number is Positive or Negative
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.