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.
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.
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);
if(number < 0) // checking conditions... for negative
printf("Number is Negative = %d",number);
if(number == 0) // checking conditions... for Zero
printf("Number is equvalent to Zero = %d",number);
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.