In Java, to check if a given number is positive or negative or 0 we can use conditional statements such as if-else or switch statement.
For example, if input number is -5, then the resulting output should be “-5 is a negative number”.
In this article, we will learn through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements (switch)
Problem Statement
We have to write a java program to check if a number is positive or negative or 0. The program should take an input number and check whether the number is positive (>0) number, negative (<0) number or zero (==0) and output the result.
Example 1:
If the input number is 13, then the output should be “13 is a positive number”.
Explanation:
Input number: 13
The input number 13 is greater than 0.
Therefore, 13 is a positive number
Example 2:
If the input number is -18, then the output should be “-18 is a negative number”.
Explanation:
Input number: -18
The input number -18 is less than the 0.
Therefore, -18 is a negative number
Logic to check a given number is positive, negative or 0
- We create a method named number which takes num1 as input.
- Inside the method, using if-else if statements, check num1
- if num1 is greater than 0 (num1>0), return 1
- if num1 is less than 0 (num1<0), return 0
- Else, return 0.
- The main method reads an integer input from the user. It calls number method with the input to determine its sign and assign it to the result variable.
- The switch() statement takes this result variable as input. It compares the result with the cases inside the switch statement.
- Prints the result and terminates using break if a case is matched.
- Uses default statement to print an error message if no cases match.
Java program to check a given number is positive, negative or 0
In the below program, we are using switch to check if a number is positive, negative or 0 in Java.
import java.util.Scanner;
public class PositiveNegativeNum {
public static int number(int num1) {
if(num1>0) {
return 1;
}
else if(num1<0) {
return -1;
}
else {
return 0;
}
}
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter the number");
int num=s.nextInt();
int result = number(num);
switch(result) {
case 0:
System.out.println(num +" is zero");
break;
case 1:
System.out.println(num +" is a positive number");
break;
default:
System.out.println(num+" is a negative number");
}
s.close();
}
}
Output:
Enter the number
183
183 is a positive number
Java program explanation to check a given number is positive, negative or 0 in Java
- The program creates a
number
method:- Takes an integer parameter
num1
. - Checks if
num1
is greater than 0. If so, it returns 1 to indicate that it’s a positive number. - Checks if
num1
is less than 0. If so, it returns -1 to indicate that it’s a negative number. - If
num1
is neither greater than nor less than 0, it returns 0 to indicate that it’s zero.
- Takes an integer parameter
- In the
main
method:- It creates a Scanner object
s
to read input from the user. - Asks the user to enter a number.
- Reads the number entered by the user.
- Calls the
number
method with the entered number as an argument. - Switches on the result returned by the
number
method. - If the result is 0, it prints that the number is zero.
- When the result is 1, it prints that the number is positive.
- If the result is neither 0 nor 1, it prints that the number is negative.
- It creates a Scanner object
- Closes the Scanner object to release system resources.
Conclusion
In this article we have learnt how to check if a number is positive, negative or 0 in Java using switch(). We have seen the logic and implemented the program using switch statement in Java. Hope you find this article is helpful.
For more related information, you can also check our other tutorials:
- Check a number is positive or negative using if-else and ternary operator in Java.
- Java program to find all natural numbers in a range
- Check character is vowel or consonant in Java
- Print all prime factors of a number in Java
Happy Learning!