C Program to Check Whether a Number is Positive or Negative

In this tutorial, I am going to show you how to check whether given number a is positive, negative, or zero in c programs.

Algorithm and Programs to Check Whether a Number is Positive or Negative in C

  • 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

Follow the below given 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 result 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 result of the above c program; as follows:

Enter a number: 12.3
Number is POSITIVE

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.