Java: Get a number from the user and print whether it is positive or negative number
Java Conditional Statement: Exercise-1 with Solution
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:

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:

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.
Java: Tips of the Day
GenerateRandomInt
Generate a random integer between Integer.MIN_VALUE and Integer.MAX_VALUE.
public static int generateRandomInt() { return ThreadLocalRandom.current().nextInt(); }
Ref: https://bit.ly/3keywpH
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook