C program to check whether a given number is positive or negative or zero; Through this tutorial, we will learn how to check whether given number a is positive, negative, or zero in c program.
C Program to Check Whether a Number is Positive or Negative
- Algorithm to Check Whether a Number is Positive or Negative
- C Program to Check Whether a Number is Positive or Negative using If Statement
- C Program to Check Whether a Number is Positive or Negative Using Nested if…else
Algorithm to Check Whether a Number is Positive or Negative
Use the following algorithm to write a c program to check whether a given number is positive or negative or zero; as follows:
- Start program
- Read the a number in program and store it into variable.
- Check the number is positive, negative or zero using if or nested if else statement.
- Print a number positivem negative or zero.
- Stop program.
C Program to Check Whether a Number is Positive or Negative using If Statement
/**
* C program to check positive negative or zero using if else
*/
#include <stdio.h>
int main()
{
int num;
/* Input number from user */
printf("Enter a number: ");
scanf("%d", &num);
if(num > 0)
{
printf("Number is POSITIVE");
}
else if(num < 0)
{
printf("Number is NEGATIVE");
}
else
{
printf("Number is ZERO");
}
return 0;
}
The output of the above c program; as follows:
Enter a number: 10 Number is POSITIVE
C Program to Check Whether a Number is 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) {
if (num == 0.0)
printf("Number is ZERO");
else
printf("Number is NEGATIVE");
}
else
printf("Number is POSITIVE");
return 0;
}
The output of the above c program; as follows:
Enter a number: 12.3 Number is POSITIVE