Press "Enter" to skip to content

This simple C program checks whether the entered number is negative or positive or zero.
Here are two types of programs. First program check for positive and negative number, here we consider zero as a positive number.



#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int num;
printf("Enter a number :");
scanf("%d",&num);
if(num<0)
printf("\n The number %d is negative", num);
else
printf("\n The number %d is Positive", num);
getch();
}

Program Logic:

The variable ‘num’ stores the entered integer type number.
The logic of positive or negative number lies with zero. If the given number is less than zero, then that should a negative number, else (that is number except negative) the number will be a positive. Here we consider zero as a positive number.

#include <stdio.h>
#include <conio.h> 
#include <stdlib.h> 
void main()
{
int num;
printf("Enter a number :");
scanf("%d",&num);
if(num<0) printf("\n The number %d is negative", num); else if (num > 0)
printf("\n The number %d is Positive", num);
else
printf("\n The entered number is zero, which not a positive nor a negative", num);
getch();
}

Code Explanation:

Here we consider the number Zero. The number will be considered as a ‘Positive’ if and only if the entered number is greater than zero. If the number is not greater than zero or less than zero, then the number should be zero, which is neither a positive nor a negative.

Sample Output:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: