Java Program to Check Whether a Number is Positive or Negative
May 31, 2023
What is Number ?
An item in mathematics used for measurement, counting, and labelling is a number. Many categories can be used to categorise numbers, including natural, whole, integer, rational, irrational, real, and complex numbers. They carry out computations and offer solutions in a number of mathematical operations, including addition, subtraction, multiplication, and division.
What is Positive and Negative Number ?
Mathematicians utilise positive and negative numbers, two different sorts of numbers, to represent values that are either greater than or less than zero. In mathematics, positive numbers are those with a value larger than zero, whereas negative numbers are those with a value lower than zero. Zero is a neutral value that denotes the lack of a numerical quantity; it is neither positive nor negative.
Steps to Check a Number is Positive or Negative :
Pick the digit you want to verify.
Comparing it to 0 Positive numbers are those that are greater than zero. A number is considered negative if it is less than zero.
The number is neither positive nor negative if it equals zero.
Pseudo Code for the above algorithm :
Input number
If number > 0 then
Print "The number is positive."
Else if number < 0 then
Print "The number is negative."
Else
Print "The number is zero."
End If
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number;
System.out.println("Enter a number: ");
number = input.nextInt();
if(number > 0) {
System.out.println("The number is positive.");
}
else if(number < 0) {
System.out.println("The number is negative.");
}
else {
System.out.println("The number is zero.");
}
input.close();
}
}
Output :
Enter a number:
12
The number is positive.
Explanation : In order to accept input from the user, we first import the java.util.Scanner class in this code. The user input is then declared as an integer variable number. We use input.nextInt and System.out.println() to ask the user to enter an integer (). Then, we use an if-else statement to determine if the number is positive, negative, or zero, and use System.out.println to print the appropriate message to the console (). Lastly, we use input to close the Scanner object ().
public class Main {
public static void main(String[] args) {
double num = -10.5;
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 neither positive nor negative.");
}
}
}
Output :
-10.5 is a negative number.
Explanation : In this illustration, a num variable is created and initialised to -10.5. The number is then tested to see if it's positive, negative, or neither using an if-else statement. If the number is greater than 0, we print "positive number" to indicate this. We indicate on the screen that a number is negative if it is less than zero. In the absence of that, we print that it is neutral. As the value of num is less than 0, when we run this programme, it will print "-10.5 is a negative number."
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment