Write a Java program to check the number is positive, negative, or zero

  • Estimate Completion Time
    not found 30 minutes
  • Difficulty Level:

Solution

import java.util.Scanner;
public class ClassName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number ");
int number = sc.nextInt();

	if (number > 0) {
System.out.println("You have entered a positive number i.e. " + number);
	} 
else if (number == 0)  {
System.out.println("You have entered a zero i.e. " + number );
		} 
else {
System.out.println("You have entered a negative number i.e. " + number);
		}
}
}
Output
Enter a number -75
You have entered a negative number i.e. -75

Explanation

  • Scanner class is used to take numbers as input from the user.
  • Conditional Statement “if-else if” is used to check the condition of whether the number entered by the user is positive, negative, or zero.
  • If a number is greater than zero, then it is positive and if the number is less than zero, then it is negative.
  • Use print statements to print according to the condition being true.
QA Ads Banner

Proleed Academy

Proleed serves / offers professionally designed IT training courses
globally.

Copyright © 2023 - Proleed Academy | All Rights Reserved.