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
Other Posts You May Like
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
- Basic Calculator Program in Java
- Java Program to Check if the given number is Odd or Even
- Java Program to Check if given Character is Vowel or Consonant
- Java Program to Find the Sum of N Numbers
- Java Program to Find Sum of Digits of a Number
Comments
Post a Comment