BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to check if Number is Positive or Negative

Last Updated: September 8, 2017 by Chaitanya Singh | Filed Under: Java Examples

In this post, we will write two java programs, first java programs checks whether the specified number is positive or negative. The second program takes the input number (entered by user) and checks whether it is positive or negative and displays the result.
→ If a number is greater than zero then it is a positive number
→ If a number is less than zero then it is a negative number
→ If a number is equal to zero then it is neither negative nor positive.

Lets write this logic in a Java Program.

Example 1: Program to check whether the given number is positive or negative

In this program we have specified the value of number during declaration and the program checks whether the specified number is positive or negative. To understand this program you should have the basic knowledge of if-else-if statement in Core Java Programming.

public class Demo
{
    public static void main(String[] args) 
    {
        int number=109;
        if(number > 0)
        {
            System.out.println(number+" is a positive number");
        }
        else if(number < 0)
        {
            System.out.println(number+" is a negative number");
        }
        else
        {
            System.out.println(number+" is neither positive nor negative");
        }
    }
}

Output:

109 is a positive number

Example 2: Check whether the input number(entered by user) is positive or negative

Here we are using Scanner to read the number entered by user and then the program checks and displays the result.

import java.util.Scanner;
public class Demo
{
    public static void main(String[] args) 
    {
        int number;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the number you want to check:");
        number = scan.nextInt();
        scan.close();
        if(number > 0)
        {
            System.out.println(number+" is positive number");
        }
        else if(number < 0)
        {
            System.out.println(number+" is negative number");
        }
        else
        {
            System.out.println(number+" is neither positive nor negative");
        }
    }
}

Output:

Enter the number you want to check:-12
-12 is negative number

Try these related Programs

  1. Java Program to Add two Numbers
  2. Java Program to Multiply two Numbers
  3. Java Program to read number from Standard input

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Peterson Number Program in Java
  3. Sphenic Number in Java – Check and Print all numbers in a range
  4. Sunny Number Program in Java
  5. Java Program to read integer value from the Standard Input

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap