Java Program to Check Whether a Number is Positive or Negative

Given a number N, we have to write a java program to check whether N is positive or negative numbers. If a number is greater than or equal to 0 then it is a positive number otherwise a negative number. There are multiple approaches of testing positive and negative numbers but here we will use if-else statement for decision making.

For Example :
4 >= 0, => 4 is a positive number.
-2 < 0, => -2 is a negative number.

Java program to check for positive and negative number

In this program, we first take an integer as input from user and store it in a variable "num". Then using a if-else statement, we check whether "num" id >= 0 or not. If yes then print appropriate message on screen.

package com.tcc.java.programs;

import java.util.Scanner;

public class PositiveNegativeNumber {

    public static void main(String[] args) {
        int num;
        Scanner scanner;
        // Take an integer from user
        scanner = new Scanner(System.in);
        System.out.println("Enter an Integer");
        num = scanner.nextInt();

        /*
         * Using if-else statement check whether
         * num is >= 0 or not.
         */
        if (num >= 0) {
            // num is positive
            System.out.println(num + " is Positive Number");
        } else {
            // num is negative
            System.out.println(num + " is Negative Number");
        }
    }
}
Output
Enter an Integer
-4
-4 is Negative Number
Enter an Integer
8
8 is Positive Number

Java program to find a number is positive or negative using a method

This java program is similar to above program except here we wrote a method "isPositive" which takes an integer as input and return true or false based of given integer is positive or negative respectively.

package com.tcc.java.programs;

import java.util.Scanner;

public class PositiveNegativeNumberFunction {

    public static void main(String[] args) {
        int num;
        Scanner scanner;
        // Take an integer from user
        scanner = new Scanner(System.in);
        System.out.println("Enter an Integer");
        num = scanner.nextInt();

        if (isPositive(num)) {
            // num is positive
            System.out.println(num + " is Positive Number");
        } else {
            // num is negative
            System.out.println(num + " is Negative Number");
        }
    }

    public static boolean isPositive(int num) {
        if (num >= 0)
            return true;
        else
            return false;
    }
}
Output
 Enter an Integer
-1
-1 is Negative Number
Enter an Integer
3
3 is Positive Number

Recommended Posts
Java Program to Check Odd or Even Numbers
Java Program to Check a Character is Vowel or Consonant
Java Program to Check Whether a Number is Palindrome or Not
Java Program to Check a Number is Prime Number or Not
Java Program to Check Perfect Number or Not
Java Program to Swap Two Numbers
Java Program to Print Fibonacci Series with and without using Recursion
Java Program to Add Subtract Multiply and Divide Two Numbers
Java Program to Find All Factors of a Number
Java Program to Print Prime Numbers between 1 to 100
Java Program to Find Sum of Digits of a Number
All Java Programs