Check if a Number is Positive, Negative, or Zero - C Program
Check if a Number is Positive, Negative, or Zero - C Program
This blog post demonstrates a simple C program that checks whether a user-entered number is positive, negative, or zero. This is a fundamental exercise in understanding conditional statements in C.
Program Code
#include <stdio.h>
int main() {
int number;
// Prompt user for input
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is positive, negative, or zero
if (number > 0) {
printf("The number %d is positive.\n", number);
} else if (number < 0) {
printf("The number %d is negative.\n", number);
} else {
printf("The number %d is zero.\n", number);
}
return 0;
}
Explanation
This program performs the following steps:
- Include the Standard I/O Library: The program includes
<stdio.h>
for input and output functions. - Read User Input: The program prompts the user to enter an integer and stores it in the
number
variable. - Check the Number: Using conditional statements, it checks if the number is greater than zero, less than zero, or equal to zero.
- Output the Result: It then prints a message indicating whether the number is positive, negative, or zero.
This program provides a basic example of how to use conditional statements to classify numeric input in C programming.
Good understanding
ReplyDelete