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 find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Add two numbers in Scala - The Coding Shala