Write a Java program that asks the user to enter a number (x). The program should determine whether this number is positive or negative and display the result.
5
Positive
import java.util.Scanner; public class PositiveNegative { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number (x): "); int x = input.nextInt(); if (x > 0) { System.out.println("Positive"); } if (x < 0) { System.out.println("Negative"); } } }
Click here to view the exercise solution
Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.
The code has been successfully copied to the clipboard.
Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!
Java basic calculator. Input numbers and operators (+, -, x, /) for calculation. If the symbol is not valid, it displays 'Character not recognized'.
Calculate and display the absolute value of a number in Java with this programming exercise on conditions.
Basic calculator with switch for mathematical operations. Allows decimal numbers and error handling. Practice your skills in Java.
Learn Java: Detect whether a number is positive or negative in this programming exercise. Enhance your Java skills.
Create a Java program that prompts the user for a letter (x) and checks whether it is a vowel or any other symbol.
Create a Java program that uses the conditional operator to assign value to a boolean variable.
Java exercise to find the largest number among three and practice Java conditions.
Create a Java program that prompts the user to enter two integers (a, b) and determines how many of them are positive using nested conditional operators in Java.
Calculate a student's grade using a switch statement in Java based on an integer input. Practice using switch, break, and default.
Practice with exercises in Java related to the conditions, loops and structures of the language.
Free programming course with practical exercises and solutions in C#. Start learning right now!
Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play
Own and third party cookies to improve our services. If you go on surfing, we will consider you accepting its use.