Find the Quadrant of Given Coordinates | Program & Explanation

Find the Quadrant of Given Coordinates | Program & Explanation

Find the Quadrant of Given Coordinates | Program & Explanation

The Cartesian plane is divided into four quadrants based on the signs of the x and y coordinates:

  1. Quadrant 1 (First Quadrant)(x > 0, y > 0)
  2. Quadrant 2 (Second Quadrant)(x < 0, y > 0)
  3. Quadrant 3 (Third Quadrant)(x < 0, y < 0)
  4. Quadrant 4 (Fourth Quadrant)(x > 0, y < 0)

Special Cases:

  • Origin (0, 0) → Lies at the center of the coordinate system.
  • X-Axis (x ≠ 0, y = 0) → Lies on the x-axis.
  • Y-Axis (x = 0, y ≠ 0) → Lies on the y-axis.

Algorithm to Determine the Quadrant

  1. Input the values of x and y.
  2. Check conditions using if-else statements:
    • If x > 0 and y > 0, it is in Quadrant 1.
    • If x < 0 and y > 0, it is in Quadrant 2.
    • If x < 0 and y < 0, it is in Quadrant 3.
    • If x > 0 and y < 0, it is in Quadrant 4.
    • If x == 0 and y == 0, it is the Origin.
    • If x == 0 and y ≠ 0, it lies on the Y-Axis.
    • If x ≠ 0 and y == 0, it lies on the X-Axis.
  3. Print the quadrant or special case based on the condition.

C Program to Find the Quadrant of Given Coordinates

cCopyEdit#include <stdio.h>

int main() {
    int x, y;

    // Input the coordinates
    printf("Enter the x and y coordinates: ");
    scanf("%d %d", &x, &y);

    // Determine the quadrant or special case
    if (x > 0 && y > 0) {
        printf("The point (%d, %d) lies in Quadrant 1.\n", x, y);
    } else if (x < 0 && y > 0) {
        printf("The point (%d, %d) lies in Quadrant 2.\n", x, y);
    } else if (x < 0 && y < 0) {
        printf("The point (%d, %d) lies in Quadrant 3.\n", x, y);
    } else if (x > 0 && y < 0) {
        printf("The point (%d, %d) lies in Quadrant 4.\n", x, y);
    } else if (x == 0 && y == 0) {
        printf("The point (%d, %d) lies at the Origin.\n", x, y);
    } else if (x == 0) {
        printf("The point (%d, %d) lies on the Y-Axis.\n", x, y);
    } else {
        printf("The point (%d, %d) lies on the X-Axis.\n", x, y);
    }

    return 0;
}

Explanation of the Program

  1. User enters values for x and y as input.
  2. Conditions check which quadrant the point belongs to.
  3. Special cases (Origin, X-Axis, Y-Axis) are handled separately.
  4. The appropriate output is printed based on the input coordinates.

Test Cases

Input (x, y)Expected Output
5 7The point (5, 7) lies in Quadrant 1.
-3 8The point (-3, 8) lies in Quadrant 2.
-4 -6The point (-4, -6) lies in Quadrant 3.
9 -2The point (9, -2) lies in Quadrant 4.
0 0The point (0, 0) lies at the Origin.
0 5The point (0, 5) lies on the Y-Axis.
-7 0The point (-7, 0) lies on the X-Axis.

Complexity Analysis

ApproachTime ComplexitySpace Complexity
If-else approachO(1)O(1)
  • The time complexity is O(1) because it involves a constant number of comparisons.
  • The space complexity is O(1) as only a few integer variables are used.

Conclusion

  • This program effectively determines the quadrant in which a point lies.
  • Handles special cases such as the Origin, X-Axis, and Y-Axis.
  • Uses if-else conditions to make comparisons efficiently.
  • Works for both positive and negative values of x and y.

This type of program is frequently asked in placement exams as