C Program to check if a Number Is Positive Or Negative
Check if a Number is Positive or Negative in C
Given an integer input, The objective is to write a code to Check if a Number is Positive or Negative in C Language..
Output : The number is Negative
Check if a Number is Positive or Negative in C
Given an integer input, the objective is check whether the given integer is Positive or Negative. In order to do so we have the following methods,
- Method 1: Using Brute Force
- Method 2: Using Nested if-else Statements
- Method 3: Using the ternary operator
We’ll discuss each method in-depth in the section below.
Method 1: Using Brute Force
This method uses Brute Force to check whether a given integer is Positive or Negative.
C Code
#include <stdio.h> int main() { int num = 23; //Conditions to check if the number is negative/positive or zero if (num > 0) printf("The number is positive"); else if (num < 0) printf("The number is negative"); else printf("Zero"); return 0; }
Output
Insert a number: 23 The number is Positive
Algorithm
For a user input num
- If the num > 0: it is a positive number.
- If the num < 0: it is a positive number.
- Else the number has to be zero itself
Same logic we have followed in the below C program.
Method 2: Using Nested if-else Statements
This method uses a nested if-else Statements to check whether a given number is Positive or Negative.
C Code
#include <stdio.h>
int main()
{
int num = -10;
//Condition to check if num is negative/positive or zero
if (num >= 0)
{
if (num == 0)
printf("The number is 0");
else
printf("The number is Positive");
}
else
printf("The number is Negative");
return 0;
}
Output
Insert a number: -10 The number is Negative
Algorithm
This method uses a nested if-else Statements to check whether a given number is Positive or Negative.
For a user input num
- If the num >= 0
- If num == 0 : num is zero
- Else number has to be positive
- Else the number has to be negative
Same logic we have followed in the below C program.
Method 3: Using Ternary Operators
This method uses a ternary operator in C to check whether a number is Positive or Negative.
C Code
#include <stdio.h> int main() { int num = -4; //Condition to check if the 0, positive or negative if(num == 0) printf("Zero"); else (num > 0) ? printf("Positive"): printf("Negative"); return 0; }
Output
Insert a number: -4 Negative
Algorithm
This method uses a ternary operator to check whether a number is Positive or Negative.
For a user input num
- If the num == 0 num is zero
- Else (num > 0) ? printf(“Positive”): printf(“Negative”);
Same logic we have followed in the below C program.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Getting Started
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
I purchased your prime Subscription and I have 4 offers. From Goldman Sachs, TCS Digital, Wipro and Capgemini
You guys are amazing best course ever.
#include
int main()
{
int num;
printf(” enter the number”);
scanf(“%d”,&num);
if (num >= 0)
{
printf(“positive”);
}
else
{
printf(” negative”);
}
return 0;
}
in which version of C platform you have typed this code. sir,plzzz tell
Version is C11