Placement Prep

Find the Quadrant of Given Coordinates: C Program and Explanation

Find the quadrant of any coordinate pair using a C program. Covers all four quadrants, origin, x-axis and y-axis cases, and O(1) complexity analysis.

By FACE Prep Team 4 min read
coordinate-geometry c-programming algorithms placement-prep data-structures

Coordinates (x, y) map to one of seven mutually exclusive outcomes: four quadrants, two axes, or the origin. Each outcome is resolved by a single if-else chain.

This problem is a placement exam staple not because the geometry is hard, but because it tests whether you handle all seven cases without gaps. Skip the origin check, and (0, 0) silently falls into an axis branch. Skip an axis check, and axis points produce the wrong quadrant. Interviewers scan for exactly those omissions. A similar coverage test applies to the sum-of-digits program, where negative input handling is the trap. Here, zero coordinates play that role.

The sections below build the solution from the mathematical rules up: sign-based quadrant definition, boundary cases, a step-by-step algorithm, a complete C program, a test-case table, and an O(1) complexity explanation.

The Four Quadrants

The Cartesian coordinate system divides the plane into four quadrants by the signs of x and y. The quadrants are numbered 1 through 4 counter-clockwise, starting from the upper-right sector.

Quadrantx signy signExample point
Quadrant 1x > 0y > 0(5, 7)
Quadrant 2x < 0y > 0(-3, 8)
Quadrant 3x < 0y < 0(-4, -6)
Quadrant 4x > 0y < 0(9, -2)

The sign of x tells you left or right of the y-axis. The sign of y tells you above or below the x-axis. That is the complete rule for all non-boundary points.

The Three Boundary Cases

Points that lie exactly on an axis, or at the intersection of both axes, belong to none of the four quadrants. There are exactly three such cases:

  • Origin: x = 0 and y = 0. Lies at the centre of the coordinate system.
  • Y-axis: x = 0 and y is not 0. Lies on the vertical axis.
  • X-axis: y = 0 and x is not 0. Lies on the horizontal axis.

Order matters here. The origin satisfies x == 0, the same condition used to detect the y-axis. If the y-axis check runs first, (0, 0) is misclassified as a y-axis point. Checking x == 0 && y == 0 together before either single-zero branch closes that gap cleanly.

Algorithm

  • Step 1: Read the integer values of x and y.
  • Step 2: If x == 0 and y == 0, the point is at the Origin.
  • Step 3: Else if x == 0 (and y is any non-zero value), the point is on the Y-axis.
  • Step 4: Else if y == 0 (and x is any non-zero value), the point is on the X-axis.
  • Step 5: Else if x > 0 and y > 0, the point is in Quadrant 1.
  • Step 6: Else if x < 0 and y > 0, the point is in Quadrant 2.
  • Step 7: Else if x < 0 and y < 0, the point is in Quadrant 3.
  • Step 8: Else (x > 0 and y < 0), the point is in Quadrant 4.

The boundary cases (Steps 2-4) come before the quadrant checks (Steps 5-8). This ordering avoids the origin-misclassification trap.

C Program to Find the Quadrant

#include <stdio.h>

int main() {
    int x, y;

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

    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 if (y == 0) {
        printf("The point (%d, %d) lies on the X-Axis.\n", x, y);
    } else 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 {
        printf("The point (%d, %d) lies in Quadrant 4.\n", x, y);
    }

    return 0;
}

The boundary checks occupy the first three branches. By the time execution reaches the quadrant branches, neither x nor y is zero, so the sign comparisons alone are sufficient. The final else does not need to state x > 0 && y < 0 explicitly. After the six prior branches, those are the only remaining values.

To handle floating-point inputs, replace int with double and %d with %lf. A reference implementation using floating-point types is available on GeeksforGeeks.

Test Cases

Re-derive every expected output from the sign rules before submitting in a placement exam. The table below covers all seven branches:

Input (x, y)Expected output
5, 7Quadrant 1
-3, 8Quadrant 2
-4, -6Quadrant 3
9, -2Quadrant 4
0, 0Origin
0, 5Y-Axis
-7, 0X-Axis

Each row exercises exactly one branch. If all seven pass, the program handles the complete input space correctly.

Time and Space Complexity

ApproachTime complexitySpace complexity
If-else chainO(1)O(1)

The program evaluates at most seven conditions regardless of the magnitude of x and y. Input size does not affect execution time. Two integer variables are the only memory beyond the program stack. Both metrics stay constant, which is why this problem is categorised as O(1) / O(1) in placement exam rubrics. For a broader treatment of how constant-space reasoning applies to other programs, see Space Complexity of Algorithms with Examples.

A useful counterpoint is the symmetric pairs in an array program, which requires O(n) time and O(n) space for the hash-map approach.

The same sign-based conditional classification that assigns a point to a quadrant is the structural seed of decision-tree splits and linear classifier boundaries in machine learning. In a trained model, the logic partitions an n-dimensional feature space rather than a 2D plane, but the if-else branching skeleton is identical to the 7-branch chain above. TinkerLLM (entry at ₹299) gives you a working environment to extend that classification idea from two dimensions to real feature vectors.

Primary sources

Frequently asked questions

What defines which quadrant a coordinate pair belongs to?

The signs of x and y alone determine the quadrant. Positive x and positive y place a point in Quadrant 1; negative x and positive y in Quadrant 2; negative x and negative y in Quadrant 3; positive x and negative y in Quadrant 4.

What happens when a point lies exactly on an axis?

A point with y = 0 and x not equal to 0 lies on the x-axis. A point with x = 0 and y not equal to 0 lies on the y-axis. Neither belongs to any of the four quadrants. The program handles both as separate branches.

Why must the origin be checked before the axis conditions?

The origin has both x = 0 and y = 0. If you check the y-axis condition (x = 0) first, the origin satisfies it and would be reported as a y-axis point. Checking x = 0 and y = 0 together, before any single-zero branch, prevents this misclassification.

Can this program handle floating-point coordinates?

Yes. Change the variable type from int to double or float, and replace %d in scanf and printf with %lf or %f. The conditional logic stays identical since it relies only on sign comparisons, not integer arithmetic.

Is finding the quadrant of coordinates a common placement exam question?

Yes. It appears regularly in aptitude and coding sections of service-sector placement tests because it tests whether a candidate can translate a multi-condition problem into a clean if-else chain with no missing cases.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF