In Java, to check if a given number is positive or negative we can use if-else-if condition or Ternary Operator. In this article, we are focusing on learning through examples, algorithm, logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to write a java program to check if a number is positive or negative. The Program should take an integer as input and It should print the result whether the number is positive number or negative number. The above action should be done in our java program. Let’s look at an example.
Example 1:
Input: 4
Output: Given number is positive
Here, for the input 4, Output is “Given number is positive” as number greater than zero.
Example 2:
Input: -4 Output: Given number is negative
Here, for the input -4, Output is “Given number is negative” as number less than zero.
Algorithm to Check Number is Positive or Negative
- Start: Begin the algorithm with input number (num)
- Check: To check whether a number is positive, number must be greater than zero.
- num>0, if the number is greater than zero ,condition satisfies and gives us positive number as the result. When this condition is not satisfied then,
- num<0 , if the number is less than zero, condition satisfies and gives us negative number as the result. If this is also not satisfied then it will say, number is zero.
- End: The algorithm terminates with the result of one of those condition check.
Logic to Check Number is Positive or Negative
There are multiple logics to check a given input is divisible by 3 or not in Java Programing language.
Logic 1: Using if-else-if
- Here we are checking our number with the help of Zero
- If number greater than zero, we say, it is positive number
- if number less than zero then we say, it is a negative number
- else, the number is zero.
if (number > 0)
System.out.println("Number is positive");
else if(number<0)
System.out.println("Number is negative");
else
System.out.println("Number is Zero");
Logic 2: Ternary Operator
- This operator takes three operands and works similar to the if else statements. If condition true, it prints first expression and if condition becomes false then it prints second expression.
- If number greater than zero, it is positive number
- number less than Zero, it is negative number else number is zero
result =(num>0)?num+" is positive" :
(num<0)?num+" is negative": num+"zero";
Java Program to Check number is Positive or Negative
Program 1: Using if-else-if
In the below program we are using if-else-if to check if a number is Positive or Negative in Java.
import java.util.Scanner;
public class PositiveNegativeNum {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("enter the number");
int num=s.nextInt();
if(num>0)
System.out.println(num+" is a positive number");
else if(num<0)
System.out.println(num+ " is a negative number");
else
System.out.println(num+ " is a Zero");
s.close();
}
}
Output:
enter the number
6
6 is a positive number
Java Program to check a number is positive or negative using if-else
- The program imports the Scanner class to facilitate user input.
- Defines a class named PositiveNegativeNum.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a number.
- Reads the integer input and stores it in ‘num’.
- Checks if ‘num’ is greater than 0, less than 0, or equal to 0.
- Prints the appropriate message indicating whether the number is positive, negative, or zero.
- Closes the Scanner object to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to check if a number is Positive or Negative in Java.
import java.util.Scanner;
public class PositiveNegativeNumTernaryOperator {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("enter the number");
int num=s.nextInt();
String result;
result=(num>0)? num+" is a positive number" :
(num<0) ?num+ " is a negative number" :
num+ " is a Zero";
System.out.println(result);
s.close();
}
}
Output:
enter the number
-9
-9 is a negative number
Java program explanation to check number is positive or negative using Ternary Operator
- The program imports the Scanner class to facilitate user input.
- Defines a class named PositiveNegativeNumTernaryOperator.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a number.
- Reads the integer input and stores it in ‘num’.
- Uses a ternary operator to assign the appropriate message to the variable ‘result’ based on whether the number is positive, negative, or zero.
- Prints the ‘result’ message.
- Closes the Scanner object to release system resources.
Conclusion
In this article, we explored how to check if a number is positive or negative using if-else-if statements and the ternary operator in Java. We understood the logic behind this and then moved on to writing the program.
For more information on related topics, you might check these articles:
- Understanding the Conditional statements in Java
- Operators in Java
- Top 10 Programming Tips in Java
We hope you find this tutorial helpful. If you have any questions or need clarification, feel free to leave a comment. Don’t forget to subscribe to our newsletter for updates on Python programming tips and tutorials.
Happy Learning!