In this article, we will write a C Program to check whether a given integer is positive or negative. For example, if user enters -5 then program should print that it’s a negative number.
C Program to check if an integer is positive or negative
In the following program, we are using if-else statement. If the input number is greater than zero then its a positive number else it is a negative number. If the number is zero then it is neither positive nor negative.
#include <stdio.h>
void main()
{
int num;
// Prompt user to enter an integer number
printf("Enter an integer number: \n");
scanf("%d", &num);
// Check if the number is positive, negative, or zero
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
return 0;
}
Output:
Output 1:
Enter an integer number:
0
0 is neither positive nor negative
Output 2:
Enter an integer number:
-3
-3 is a negative number
Output 3:
Enter an integer number:
100
100 is a positive number
Leave a Reply