Check if a Number is Positive, Negative, or Zero - C Program

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:

  1. Include the Standard I/O Library: The program includes <stdio.h> for input and output functions.
  2. Read User Input: The program prompts the user to enter an integer and stores it in the number variable.
  3. Check the Number: Using conditional statements, it checks if the number is greater than zero, less than zero, or equal to zero.
  4. 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.

Comments

Post a Comment

Popular Posts