In this blog post, you will learn how to write a C program to check the given number positive or negative? This question helps you to increase your logical skill. You will get the answer to the following questions;
- How do you find a given number is positive or negative?
- How do you check if a number is a positive integer in C?
- Write the C program to take input any number from the user and check whether the given number is positive or negative using the bitwise Operators and ternary operators.
How to determine a number is positive or negative?
Using the relational operators (< or >) you can check a number is positive or negative. A number that is greater than zero is termed a positive number whereas the number less than zero is the negative number Let see an example,
- A number ‘n’ is said negative if it is less than 0 i.e. n< 0.
- A number ‘n’ is said positive if it is greater than 0 i.e. n> 0.
But using the MSB (most significant bit) you can also check number is positive or not.
- A number ‘n’ is said negative if MSB of an integer is 1.
- A number ‘n’ is said positive if MSB of an integer is 0.
C program to check given number positive or negative:
Method 1: Using the if-else and relational operator
You can check whether a number is positive or negative with the help of a relational operator. See the below code.
#include <stdio.h> int main() { int sign = 0; int data = 0; printf("\n\n Enter the number = "); scanf("%d",&data); //Get the number sign = (data > 0) - (data < 0); // check the sign of the number if(sign == 1) { printf("\n\n Enter number is a positve number\n"); } else if(sign == -1) { printf("\n\n Enter number is a negative number\n"); } else { printf("\n\n Enter number is zero\n"); } return 0; }
Output:
Method 2: C program to find Positive or Negative Number using MSB:
In the below code, we are checking the MSB bit of a given number. If MSB bit 1, then the number is negative otherwise positive.
#include <stdio.h> // Total bits required to represent integer #define BITS sizeof(int) * 8 int main() { int num; printf("Enter any number: "); scanf("%d", &num); (num & (1 << (BITS - 1)))? printf("Negative number\n"):printf("Positive number\n"); return 0; }
Output:
Enter any number: -10
Negative number
Method-3: C program to find Positive or Negative number using Conditional Operator
This program asks the user to enter any number. Next, this C program checks whether the given value is positive or negative or zero using Conditional Operator or Ternary operator.
#include <stdio.h> int main() { int sign = 0; int data = 0; printf("\n\n Enter the number = "); scanf("%d",&data); //Get the number // check the sign of the number sign = (data > 0) - (data < 0); (sign > 0)? printf(" Number is Positive \n"):printf(" Number is Negative \n"); return 0; }
Output:
Enter any number: 10
Positive number
Recommended Post:
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- C Program to check if a number is positive, negative or zero using bit operators
- C Program to check positive or negative without using conditional statements
- Program to calculate the power of a number
- C program to check whether a character is a vowel or consonant
- C Program to find number of denominations for a given amount.