Coding Practice

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

Check whether a number is negative, positive or zero
Sample Output
Check whether a number is negative, positive or zero.

Enter a number: 7
7 is positive number.
Java-Source Code
//package ifelsejava;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int number;

        Scanner input = new Scanner(System.in);

        System.out.println("Check whether a number is negative, positive or zero.\n");
        
        System.out.print("Enter a number: ");
        number = input.nextInt();

        if (number > 0) {
            System.out.println(number + " is positive number.");
            
        } else if(number < 0){
            System.out.println(number + " is negative number.");
            
        } else if(number == 0){
            System.out.println("Zero Detected!");
            
        } else{
            System.out.println("Invalid input!");
        }
    }
}
Sample Output
Check whether a number is negative, positive or zero.

Enter a number: -100
-100 is negative number.

No comments:

Post a Comment

Change Theme
X