POSITIVE NUMBER OR NEGATIVE NUMBER IN C PROGRAMMING

Positive or Negative Number Check in C

Positive or Negative Number Check in C - Program and Explanation

Program:

#include <stdio.h>

int main() {
    // Declare a variable to store the user's input
    int number;

    // Prompt the 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 is zero.\\n");
    }

    return 0;
}
    

Explanation:

#include <stdio.h>: This line includes the standard input/output library in C, which allows you to use printf and scanf functions.

int main(): This is the main function where the execution of the program begins. The int before main() indicates that this function returns an integer value.

int number;: Declares an integer variable named number to store the user's input.

printf("Enter a number: ");: This line prompts the user to enter a number.

scanf("%d", &number);: This line reads the user's input and stores it in the number variable.

if (number > 0): Checks if the number is greater than 0. If true, the number is positive.

else if (number < 0): Checks if the number is less than 0. If true, the number is negative.

else: This handles the case where the number is neither positive nor negative, which means it must be zero.

return 0;: Indicates that the program has ended successfully. Returning 0 is a convention in C that signifies successful execution.

Comments

Popular Posts