In this tutorial you will learn about the C program to check number is positive negative or zero and its application with practical example.
In this tutorial, we will learn to create a C program that will find weather a number is positive, negative or Zero using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- C Operators
- If-else statement.
- Nested else-if
- Basic c programming.
- Basic input/output.
C program to check whether a number is positive, negative or zero.
In this program we will find whether a number is positive, negative or zero series using if else statement. We would first declared and initialized the required variables. Next, we would prompt user to input any number. Later we will find that number is positive,negative or zero.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <stdio.h> int main() { int number; // declaring variables printf("Enter any number: "); scanf("%d", &number); // taking value of number.. if(number> 0) // checking conditions... for positive { printf("Number is Positive = %d",number); } else { if(number < 0) // checking conditions... for negative { printf("Number is Negative = %d",number); } else { if(number == 0) // checking conditions... for Zero { printf("Number is equvalent to Zero = %d",number); } } } return 0; } |
Output
Positive Value.
Negative Value
Zero Value.
In the above program, we have first declared and initialized a set variables required in the program.
- number = it will hold the value of number.
In the next statement user will be prompt to enter any number terms which will be assigned to variable ‘number’.
For finding given integer is positive ,negative or zero we will use We will use nested if else statement
in above logic inside if we check if(number > 0), number is greater than zero then number is positive.
if(number < 0) if number less than zero then number is negative. and if
if(number == 0), then number is zero.
Here the C program to find whether a number is positive, negative or zero.The above approach is easiest way find the problem. First we check condition for positive number, then for negative or and later we find for zero.