Check if a Number is Positive or Negative in Java
Check if a Given Number is Positive or Negative in Java
Given an integer num input the objective is to write a code to Check if a Given Number is Positive or Negative in Java Language.
Example Input : num = 11 Output : Positive
Check if a Number is Positive or Negative in Java
In this article, we will create a java program to check whether the number entered by the user is positive or negative. The number is demonstrated as positive or negative by comparing the entered number with the Zero(0). If the number entered by the user is greater than zero, then the number is positive, else if the number is less than zero, then the number is negative and else the number is zero.
- N > 0 then, number is Positive.
- N < 0 then, number is Negative.
- N = 0 then, number is Zero.
To solve the above problem we write a Java code using three different methods.
- Method 1: Using Brute Force
- Method 2: Using Nested if-else Statements
- Method 3: Using Ternary Operators
The Above methods are discussed in depth in the sections below.
Method 1: Using Brute Force
This method uses Brute Force to check whether a given integer is Positive or Negative.
Java Code
//Brute force 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"); } }
Output
The number is positive
Algorithm
This method uses Brute Force to check whether a given integer is Positive or Negative. The Algorithm for the above code is as follows
- Step 1. Start
- Step 2. Insert the number.
- Step 3. If the number is greater than Zero then print “The number is Positive”
- Step 4: If the number is smaller than zero, then print, “The number is Negative”
- Step 5. Else print, “The number is Zero”
- Step 6. Stop
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.
Java Code
//Using Nested If-else statement 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"); } }
Output
The number is positive
Algorithm
This method uses a nested if-else Statements to check whether a given number is Positive or Negative.
- Step 1 – Start
- Step 2 – Insert the number.
- Step 3 – If the number is greater or equal move to the inner nested loop
- Step 3.1 – If the number is zero, print Zero
- Step 3.2 – Else print The Number is Positive
- Step 4 – Else the number has to be negative, Print The number is Negative
- Step 5 – Stop
Method 3: Using Ternary Operator
This method uses a ternary operator to check whether a number is Positive or Negative.
Java code
//Using Ternary Operator 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); } } }
Output
Zero
Algorithm
This method uses a ternary operator to check whether a number is Positive or Negative. The Algorithm for the above code is as follows,
- Step 1 – Start
- Step 2 – Insert the number.
- Step 3 – If number is equal to zero, Print Number is Zero
- Step 4 – Else do following – (num > 0) ? cout << “Positive”: cout << “Negative”;
- Step 5 – Stop
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
- Perfect Square : 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
Login/Signup to comment