;

Java Program to Check Whether a Number is Positive or Negative


Tutorialsrack 19/04/2021 Java

In this java program, you’ll learn how to check whether a number is positive or negative in java. We are done by using a if..else statement in Java. A Number is positive if the number is greater than 0 (number > 0). And if the number is less than 0 then it is a negative number. 

Java Program to Check Whether a Number is Positive or Negative

Java Program to Check Whether a Number is Positive or Negative
//Java Program to Check Whether a Number is Positive or Negative

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

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

-15 is negative number

 

Enter the number you want to check:111

111 is positive number


Related Posts



Comments

Recent Posts
Tags