# Find Quadrant of The Coordinate Point in Java

The Cartesian coordinate system is a fundamental framework in mathematics, defining the position of points in a plane. It is divided into four quadrants, each determined by the signs of the coordinates. Identifying the quadrant is crucial in various applications, including computer graphics, robotics, and navigation. Points can also lie on the x-axis, y-axis, or at the origin.

Identifying the quadrant in which a point resides is a common task in various applications, from computer graphics to robotics and navigation. The quadrants are defined as follows:

1st Quadrant: Both x and y are positive.

2nd Quadrant: x is negative, and y is positive.

3rd Quadrant: Both x and y are negative.

4th Quadrant: x is positive, and y is negative.

Example1:

Input: (x,y) = (3, 4)

Example2:

Input: (x,y) = (0, -5 )

Output:  Y-axis

## Using Naive Approach

### Algorithm

Step 1: Hardcode values for x and y.

Step2: Print "1st Quadrant" if x > 0 and y > 0.

Step3: Print "2nd Quadrant" if x < 0 and y > 0.

Step4: Print "3rd Quadrant" if x < 0 and y < 0.

Step5: Print "4th Quadrant" if x > 0 and y < 0.

Step6: Print "x axis" if x!= 0 and y == 0.

Step7: Print "y axis" if x == 0 and y == 0.

Step8: Print "Origin" if x == 0 and y == 0.

Here is an implementation of finding the quadrant of the coordinate point using a naive approach:

FileName:NaiveApproach.java

`public class NaiveApproach { public static void main(String[] args) { // Hardcoding the coordinates int x = 1; int y = 2; // Determine the quadrant or axis using a naive approach if (x > 0 && y > 0) { System.out.println("1st Quadrant"); } else if (x < 0 && y > 0) { System.out.println("2nd Quadrant"); } else if (x < 0 && y < 0) { System.out.println("3rd Quadrant"); } else if (x > 0 && y < 0) { System.out.println("4th Quadrant"); } else if (x != 0 && y == 0) { System.out.println("x-axis"); } else if (x == 0 && y != 0) { System.out.println("y-axis"); } else if (x == 0 && y == 0) { System.out.println("Origin"); } }}`

Output:

`1st Quadrant`

Complexity analysis: The time complexity of the above approach is O(1) because the code performs a constant number of operations regardless of the input values, and the space complexity is O(1) because the code uses a fixed amount of additional space (a few integer variables) regardless of the input values.

## Using Optimized Approach

### Algorithm

Step 1: Hardcode values for x and y.

Step 2: Check if the point is at the origin.

Step 3: Set the result to "Origin" if x == 0 and y == 0.

Step 4: Check if the point is on the y-axis.

Step 5: Set the result to "y axis" if x == 0 and y != 0.

Step 6: Check if the point is on the x-axis.

Step 7: Determine the quadrant by calculating the product of the signs of x and y.

Step 8: Use a switch statement to assign the quadrant based on the product.

Step 9: Output the result.

Here is an implementation of finding the quadrant of the coordinate point using an optimized approach:

FileName:OptimizedApproach.java

`public class OptimizedApproach{ public static void main(String[] args) { // Hardcoding the coordinates int x = 1; int y = 2; // Determine the quadrant or axis using an optimized approach String result; if (x == 0 && y == 0) { result = "Origin"; } else if (x == 0) { result = "y-axis"; } else if (y == 0) { result = "x-axis"; } else { int quadrant = (x > 0 ? 1 : -1) * (y > 0 ? 1 : -1); switch (quadrant) { case 1: result = "1st Quadrant"; break; case -1: if (x > 0) { result = "4th Quadrant"; } else { result = "3rd Quadrant"; } break; case -3: result = "2nd Quadrant"; break; default: result = "Unknown";  } } System.out.println(result); }}`

Output:

`1st Quadrant`

Complexity analysis: The time complexity of the above approach is O(1) because the code performs a constant number of operations regardless of the input values, and the space complexity is O(1) because the code uses a fixed amount of additional space (a few integer and string variables) regardless of the input values.