Java Program to check if Number is Positive or Negative

In this tutorial, we will learn how to write a Java program to check whether a number is positive or negative.

Java Program to Check if Number is Positive or Negative

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to check if Number is Positive or Negative
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            System.out.print("Enter the number you want to check:");
            int number = scanner.nextInt();
            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:-5
-5 is negative number

Related Java Programs

Comments