## Check if a number is Positive or Negative

This is very important question for beginners who wants to start competitive programming form absolutely zero position. In this article you will be able to learn how to use if – else conditions in every programming language to solve problem with greater efficiency.

In this competitive programming question you will be given a number as input and you have to create program such that it will return whether the number is positive is negative using if – else conditions in program.

Now let’s try to design solution first. The number is said to positive when it is greater than 0 and similarly it is called negative when it is smaller than 0. Hence we got two conditions for our program. But there is one more possibility that is number can be 0 itself, so in that case we have to return “number is zero”. SO to summarize this we can say that,

• N > 0 then, number is Positive.
• N < 0 then, number is Negative.
• N = 0 then, number is Zero.

There are three methods by which we can solve this program :

• Method 1: Using Brute Force
• Method 2: Using Nested if-else Statements
• Method 3: Using Ternary Operators

### Method 1 : Brute Force

```				```
// C program to check number is positive or negative
#include <stdio.h>
int main()
{
int num = 54;

//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;
}
```
```
```				```
// C++ program to check number is positive or negative
#include <iostream>
using namespace std;

int main()
{
int num = 96;

//Conditions to check if the number is negative or positive
if (num > 0)
cout << "The number is positive";
else if (num < 0)
cout << "The number is negative";
else
cout << "Zero";

return 0;
}
```
```
```				```
// Java Brute force program to find number is positive
// or negative
class Main
{
public static void main (String[]args)
{

int num = 5;

//Conditions to check if the number is negative or positive
if (num > 0)
System.out.println ("The number is positive");
else if (num < 0)
System.out.println ("The number is negative");
else
System.out.println ("Zero");
}
}
```
```
```				```
# Python program to check number is positive or not
num = 15
if num > 0:
print('Positive')
elif num < 0:
print('Negative')
else:
print('Zero')
```
```

### Method 2 : Nested if - else statement

```				```
// C program for +ve ir -ve number
#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;
}
```
```
```				```
// C++ Code
#include<iostream>
using namespace std;

int main()
{
int num = -12;

//Condition to check if the number is negative or positive
if (num >= 0)
{
if (num == 0)
cout << "Zero";
else
cout << "The number is positive";
}
else
cout << "The number is negative";

return 0;
}
```
```
```				```
// Using Nested If-else statement in Java
class Main
{
public static void main (String[]args)
{

int num = 5;

//Condition to check if the number is negative or positive
if (num >= 0)
{
if (num == 0)
System.out.println ("Zero");
else
System.out.println ("The number is positive");
}
else
System.out.println ("The number is negative");

}
}
```
```
```				```
# Python Code
num = 15
if num>=0:
if num==0:
print('Zero')
else:
print("Positive")
else:
print("Negative")
```
```

### Method 3 : Using the ternary operator

```				```
// 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;
}
```
```
```				```
// C++ Code

#include <iostream>
using namespace std;

int main()
{
int num = -15;

//Condition to check if the 0, positive or negative

if(num == 0)
cout << "Zero"; else (num > 0) ? cout << "Positive": cout << "Negative";

return 0;
}
```
```
```				```
//Using Ternary Operator in Java
class Main
{
public static void main (String[]args)
{

int num = 0;

//Condition to check if the number is negative or positive

if (num == 0)
{
System.out.println ("Zero");
}

else{
String result = num > 0 ? "The number is positive" : "The number is negative";
System.out.println (result);
}
}
}
```
```
```				```
# Python Code using Ternary Operator
num =15
print("Positive" if num>=0 else "Negative")
```
```
Categories: TCQ NQT Preparation