Java Online Compiler
Example: Determine Quadrant of Coordinates in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Determine Quadrant of Coordinates import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Create a Scanner object to read user input Scanner scanner = new Scanner(System.in); // Step 2: Prompt the user to enter the x-coordinate System.out.print("Enter the x-coordinate: "); double x = scanner.nextDouble(); // Step 3: Prompt the user to enter the y-coordinate System.out.print("Enter the y-coordinate: "); double y = scanner.nextDouble(); // Step 4: Determine the quadrant or position of the point String result; if (x == 0 && y == 0) { result = "The point (" + x + ", " + y + ") is at the Origin."; } else if (x == 0) { result = "The point (" + x + ", " + y + ") lies on the Y-axis."; } else if (y == 0) { result = "The point (" + x + ", " + y + ") lies on the X-axis."; } else if (x > 0 && y > 0) { result = "The point (" + x + ", " + y + ") lies in Quadrant I."; } else if (x < 0 && y > 0) { result = "The point (" + x + ", " + y + ") lies in Quadrant II."; } else if (x < 0 && y < 0) { result = "The point (" + x + ", " + y + ") lies in Quadrant III."; } else { // x > 0 && y < 0 result = "The point (" + x + ", " + y + ") lies in Quadrant IV."; } // Step 5: Print the determined position System.out.println(result); // Step 6: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS