adplus-dvertising

C program to check whether a number is positive or negative

frame-decoration

C program to check whether a number is positive or negative

Problem Description

This c program ask user to input a number and then checks whether the entered number is positive, negative or zero. The logic for this C program implementation is very simple. It just compare the number with zero and on this basis, it decides whether the number is positive, negative or zero.

C program to check whether a number is positive or negative - Source code
     
                
  #include <stdio.h>

void main()
{
    int num;

    printf("Enter a number: \n");
    scanf("%d", &num);
    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("You have entered 0.");
}
     
      

Program Output

Case 1:

Enter a number:
689
689 is a positive number


Case 2:

Enter a number:
-65
-65 is a negative number

Case 3:

Enter a number:
0
You have entered 0.

Program Explanation

1. The first if statement checks whether the number is greater than 0 or not. If true, it prints the output on the screen.

2. The second statement, else if statement, checks if the number is less than than 0 and if true, the block inside it executes.

3. At last, if neither the entered number is greater than 0 nor less than 0, then it is obvious that it is 0. In such case, the last else block gets executed.