Java Program to Check if given Number is Positive or Negative - The Coding Shala

Home >> Java Programs >> Check if the given number is positive or negative

 In this post, we will learn How to write a Java program to check if the given number is positive or negative.

Java Program to Check if given Number is Positive or Negative

Write a Java program to check if the given number is positive or negative.

Example 1:
Input: 22
Output: 22 is a positive number

Approach:

If the given number is smaller than 0 then it's negative otherwise it is positive, using if condition we will check that.

Java Program: 

/**
 * https://www.thecodingshala.com/
 */

import java.util.Scanner;

public class Main {

    public static void checkNum(int num) {
        if (num < 0) {
            System.out.println(num + " is a negative number");
        } else {
            System.out.println(num + " is a positive number");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        // considering integers only
        int num = sc.nextInt();
        checkNum(num);
    }
}

Output: 

Enter the number
22
22 is a positive number


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Java Program to Find GCD or HCF of Two Numbers - The Coding Shala