w3resource
Java Programming Exercies

Java Conditional Statement Exercises: Check the number is negative, zero, or positive

Java Conditional Statement: Exercise-27 with Solution

Write a Java program that reads an integer and check whether it is negative, zero, or positive.

Test Data
Input a number: 7

Pictorial Presentation:

Java conditional statement Exercises: Check the number is negative, zero, or positive

Sample Solution:

Java Code:

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

        if (n > 0)
        {
            System.out.println("Number is positive");
        }
        else if (n < 0)
        {
            System.out.println("Number is negative");
        }
        else
        {
            System.out.println("Number is zero");
        }
    }
}

Sample Output:

Input a number: 7                                                                                             
Number is positive

Flowchart:

Flowchart: Java Conditional Statement Exercises - Check the number is negative, zero, or positive

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java program to display the following character rhombus structure.
Next: Reads a floating-point number and print result

What is the difficulty level of this exercise?