Skip to main content

Command Palette

Search for a command to run...

Java Programming: Positive, Negative or Zero

Updated
1 min readView as Markdown
Java Programming: Positive, Negative or Zero

Example

Input
enter the number 5
Output 
The number is positive

Required to Know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out.print(), Use of If-else, and Use of Data types.

Steps

  • Take the number as an input from the user.

  • Check if the number is positive or negative or Zero.

    • If the number is greater than zero, print positive.

    • If the number is less than zero, print negative.

    • Otherwise, print zero.

Condition.

if(num>0){
    print("The number is positive")
}
elif(num<0){
    print("The number is negative")
}
else{
    print("The number is zero")
}

Program

import java.util.Scanner;

public class zero {

    /**
     * This class checks if a number is positive, negative, or zero.
     */

    public static void main(String[] args) {

        // Create a Scanner object to read user input.
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter a number.
        System.out.print("Enter a number: ");
        int a = sc.nextInt();

        // Check if the number is positive.
        if (a > 0) {

            // The number is positive.
            System.out.println("The number is positive");

        } else if (a < 0) {

            // The number is negative.
            System.out.println("The number is negative");

        } else {

            // The number is zero.
            System.out.println("The number is zero");

        }

    }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.