Write a C program to find that the accepted number is Positive Negative or Zero.

This is one of the easiest program if you know how to deal with if-else statements in C language.

Tutorial 📺

Watch tutorial and learn how to solve this problem. 😉

Steps 🪜

To find if number is Positive or Negative or Zero

  1. Accept an integer from the user and store the value in a variable ‘num‘.
  2. Here, we will use if-else statements to find out if the accepted number is positive, negative or zero.
  3. If the value stored in variable num is greater than 0 (num>0) then display the message “Number is POSITIVE”.
  4. In else-if part, test if the value stored in variable num is lesser than 0 (num<0), if yes then display the message “Number is NEGATIVE” otherwise go to the else part.
  5. Now, if the value is neither positive nor negative, it means it is zero (0) for sure. Display the message “Number is ZERO”, in else part.

Flow Chart 🌻

Check how to draw flow chart for this program.

Find if number is positive, negative or zero

Code 💻

#include <stdio.h>
#include <conio.h>

void main()
{
  int num=0;
  clrscr();
  printf("Please enter the number: ");
  scanf("%d", &num);
  if(num > 0) // POSTIVE: If number is greater than 0
    printf("\n%d: Number is POSITIVE.", num);
  else if(num < 0) // NEGATIVE: If number is lesser than 0
    printf("\n%d: Number is NEGATIVE.", num);
  else // Otherwise the number is 0
    printf("\n%d: Number is ZERO.", num);
  getch();
}

Example 😍

Let’s assume the user enters the value 20 which is stored in variable num.

Since, the value stored in num is greater than 0, it will display the output as:

Number is POSITIVE.

If the user enters the value -3 which we know is lesser than 0, it will display the message:

Number is NEGATIVE.

But, what if the user enters 0 (zero)? Since, it is neither positive nor negative. Thus, it will execute the else part and will display the message:

Number is ZERO.

This is how we find out if the number is Positive, Negative or Zero.
For more C programs, click here.

Download the entire post to read it offline.


Popular Posts 😃

Categorized in: