Java Program to Check Whether a Number is Positive or Negative
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
Example 1 :
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.
Example 2 :
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.
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
Login/Signup to comment