C Program : Check a given integer is positive or negative
#include <stdio.h>
int main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (num > 0) { printf("%d is a positive integer.\n", num); } else if (num < 0) { printf("%d is a negative integer.\n", num); } else { printf("The number is zero.\n"); } return 0; }In this program:
- We include the standard input/output header file
stdio.h. - We declare an integer variable
numto store the input integer. - We prompt the user to enter an integer using
printfand read the input usingscanf. - We use an
if-elsestatement to check if the value ofnumis positive, negative, or zero. - If
numis greater than 0, it is considered a positive integer, and we print a message accordingly. - If
numis less than 0, it is considered a negative integer, and we print a message accordingly. - If
numis neither positive nor negative (i.e., it's equal to 0), we print a message indicating that it's zero. - The program returns 0 to indicate successful execution.
Compile and run this program, and it will determine whether the input integer is positive, negative, or zero.
Comments
Post a Comment