w3resource

Java: Get a number from the user and print whether it is positive or negative number

Java Conditional Statement: Exercise-1 with Solution

Check Positive or Negative Number

Write a Java program to get a number from the user and print whether it is positive or negative.

Test Data
Input number: 35

Pictorial Presentation:

Java conditional statement Exercises: Program to check  positive or negative number

Sample Solution:

Java Code:

import java.util.Scanner;
public class Exercise1 {

    
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Input number: ");
        int input = in.nextInt();

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

Sample Output:

Input number: 35                                                                                              
Number is positive 

Flowchart:

Flowchart: Java Conditional Statement Exercises - Program to check  positive or negative number

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Java Conditional Statement Exercises
Next: Write a Java program to solve quadratic equations (use if, else if and else).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/conditional-statement/java-conditional-statement-exercise-1.php