C Program to Check Whether a Number is Positive or Negative

In this post, you will learn how to check whether a number is positive or negative using C Programming language.

C Program to Check Whether a Number is Positive or Negative

If a number is greater than zero, then it will be a Positive Number. Similarly, if a number is smaller than zero, then it will be a Negative Number. 

Keep in mind that 0 is neither a Positive nor Negative.

So, Without further ado, let’s begin the tutorial.

C Program to Check Whether a Number is Positive or Negative

// C Program To Check Whether a Number is Positive or Negative
#include <stdio.h>
int main(){
    int num;
    
    // Asking for Input
    printf("Enter an Integer: ");
    scanf("%d", &num);
    
    // logic
    if (num > 0){
        printf("%d is a Positive Number.", num);
    }
    else if (num < 0) {
        printf("%d is a Negative Number.", num);
    }
    else {
        printf("0 is neither Positive nor Negative.");
    }
    return 0;
}

Output 1

Enter an Integer: 18
18 is a Positive Number.

Output 2

Enter an Integer: 0
0 is neither Positive nor Negative.

Output 3

Enter an Integer: -7
-7 is a Negative Number.

How Does This Program Work ?

    int num;

In this program, we have declared an int type variable named as num.

    // Asking for Input
    printf("Enter an Integer: ");
    scanf("%d", &num);

Then, the user is asked to enter an integer. This number will be stored in the num variable.

    // logic
    if (num > 0){
        printf("%d is a Positive Number.", num);
    }

Now, the logic part of the program Starts.

If the number entered by the user is greater than 0, then it will be a Positive Integer. 

    else if (num < 0) {
        printf("%d is a Negative Number.", num);
    }

If the number is smaller than 0, then it will be a Negative Number.

    else {
        printf("0 is neither Positive nor Negative.");
    }

And, if the number is neither greater nor smaller than 0, then it means the number is equal to Zero. In this case our program will display 0 is neither Positive nor Negative.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen. 

> – Checks If the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes True.

< – Checks If the value of the right operand is greater than the value of the left operand. If yes, then the condition becomes True.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

// – Used for Commenting in C. 

Conclusion

I hope after reading this post, you understand how to check whether a number is Positive or Negative using C Programming language.

If you have any doubts regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published.