In this article, we will understand how to check whether a number is positive or negative. This is accomplished by checking if the given number is greater or less then 0.
Below is a demonstration of the same −
Input
Suppose our input is −
Output
The desired output would be −
The number -30 is a negative numberAlgorithm
Step 1 - START Step 2 - Declare an integer values namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 - Using an if-else condition, check my_input > 0 value. Step 5 - If the result is true, then it’s a positive number, else it’s a negative number. Step 6 - Display the result Step 7 - StopExample 1
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool
Output
Required packages have been imported A reader object has been defined Enter the number : -30 The number -30 is a negative numberExample 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
Output
The number is defined as -30 The number -30 is a negative number
Updated on 22-Feb-2022 10:52:36
- Related Questions & Answers
- C program to Check Whether a Number is Positive or Negative or Zero?
- Python Program to Check if a Number is Positive, Negative or 0
- Java program to find if the given number is positive or negative
- How to check if a number is positive, negative or zero using Python?
- Program to check if a number is Positive, Negative, Odd, Even, Zero?
- Java Program to Check Whether a Number is Even or Odd
- Java Program to Check Whether a Number is Prime or Not
- Check whether product of integers from a to b is positive, negative or zero in Python
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- Check if a number is positive, negative or zero using bit operators in C++
- Java Program to convert positive int to negative and negative to positive
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to Check Whether a Number is Palindrome or Not
- C Program to Check Whether a Number is Prime or not?
- Program to check whether a number is Proth number or not in C++
Read a number from the user using the Scanner class's method. check whether the given number is greater, lesser or, equal to 0. If it is greater given number is positive if the lesser given number is negative. the else given number is neither positive or negative.
Example
import java.util.Scanner; public class PositiveOrNegative { public static void main(String args[]){ int num; System.out.println("Enter a number ::"); Scanner sc = new Scanner(System.in); num = sc.nextInt(); if (num > 0){ System.out.println("Given number is a positive integer"); } else if(num < 0){ System.out.println("Given number is a negative integer"); } else { System.out.println("Given number is neither positive nor negative integer"); } } }Output 1
Enter a number :: 55 Given number is a positive integerOutput 2
Enter a number :: -88 Given number is a negative integer
Updated on 13-Mar-2020 07:03:14
- Related Questions & Answers
- Python Program to Check if a Number is Positive, Negative or 0
- Java Program to Check Whether a Number is Positive or Negative
- C program to Check Whether a Number is Positive or Negative or Zero?
- How to check if a number is positive, negative or zero using Python?
- Program to check if a number is Positive, Negative, Odd, Even, Zero?
- C program to find if the given number is perfect number or not
- Java Program to convert positive int to negative and negative to positive
- C# Program to check if a number is Positive, Negative, Odd, Even, Zero
- Check if a number is positive, negative or zero using bit operators in C++
- Java program to find if the given number is a leap year?
- Java program to find whether given number is even or odd
- C Program to find the given number is strong or not
- Sum a negative number (negative and positive digits) - JavaScript
- Python program to check if the given number is Happy Number
- Java program to find whether the given string is empty or null