C program to find in which quadrant the coordinates lie.

In C programming, determining which quadrant coordinates lie in is a fundamental problem in coordinate geometry. A coordinate system is divided into four quadrants based on the signs of the x and y coordinates.

Syntax

if (x > 0 && y > 0)
    // First quadrant
else if (x < 0 && y > 0)
    // Second quadrant
else if (x < 0 && y < 0)
    // Third quadrant
else if (x > 0 && y < 0)
    // Fourth quadrant
else
    // Origin or on axis

Quadrant Rules

Quadrant II (-x, +y) Quadrant I (+x, +y) Quadrant III (-x, -y) Quadrant IV (+x, -y) +Y +X -Y -X Origin (0,0)
  • First Quadrant: Both x and y are positive (+x, +y)
  • Second Quadrant: x is negative, y is positive (-x, +y)
  • Third Quadrant: Both x and y are negative (-x, -y)
  • Fourth Quadrant: x is positive, y is negative (+x, -y)

Example

Following is the C program to find the quadrant in which the given coordinates lie −

#include <stdio.h>

int main() {
    int x, y;
    
    printf("Enter x and y coordinates: ");
    scanf("%d %d", &x, &y);
    
    if (x > 0 && y > 0) {
        printf("Point (%d, %d) lies in the 1st Quadrant<br>", x, y);
    }
    else if (x < 0 && y > 0) {
        printf("Point (%d, %d) lies in the 2nd Quadrant<br>", x, y);
    }
    else if (x < 0 && y < 0) {
        printf("Point (%d, %d) lies in the 3rd Quadrant<br>", x, y);
    }
    else if (x > 0 && y < 0) {
        printf("Point (%d, %d) lies in the 4th Quadrant<br>", x, y);
    }
    else if (x == 0 && y == 0) {
        printf("Point (%d, %d) is at the Origin<br>", x, y);
    }
    else if (x == 0) {
        printf("Point (%d, %d) lies on the Y-axis<br>", x, y);
    }
    else {
        printf("Point (%d, %d) lies on the X-axis<br>", x, y);
    }
    
    return 0;
}
Enter x and y coordinates: 5 3
Point (5, 3) lies in the 1st Quadrant

Example with Multiple Test Cases

Here's a program that demonstrates all possible cases −

#include <stdio.h>

void checkQuadrant(int x, int y) {
    printf("Point (%d, %d): ", x, y);
    
    if (x > 0 && y > 0)
        printf("1st Quadrant<br>");
    else if (x < 0 && y > 0)
        printf("2nd Quadrant<br>");
    else if (x < 0 && y < 0)
        printf("3rd Quadrant<br>");
    else if (x > 0 && y < 0)
        printf("4th Quadrant<br>");
    else if (x == 0 && y == 0)
        printf("Origin<br>");
    else if (x == 0)
        printf("Y-axis<br>");
    else
        printf("X-axis<br>");
}

int main() {
    /* Test various coordinates */
    checkQuadrant(2, 3);    /* 1st quadrant */
    checkQuadrant(-4, 6);   /* 2nd quadrant */
    checkQuadrant(-5, -3);  /* 3rd quadrant */
    checkQuadrant(7, -2);   /* 4th quadrant */
    checkQuadrant(0, 0);    /* Origin */
    checkQuadrant(0, 5);    /* Y-axis */
    checkQuadrant(4, 0);    /* X-axis */
    
    return 0;
}
Point (2, 3): 1st Quadrant
Point (-4, 6): 2nd Quadrant
Point (-5, -3): 3rd Quadrant
Point (7, -2): 4th Quadrant
Point (0, 0): Origin
Point (0, 5): Y-axis
Point (4, 0): X-axis

Key Points

  • Use && (logical AND) to check both coordinate conditions simultaneously
  • Handle special cases: origin (0,0), x-axis (y=0), and y-axis (x=0)
  • The order of if-else conditions matters for correct logic flow

Conclusion

Determining quadrants in C involves checking the signs of x and y coordinates using conditional statements. This fundamental concept is essential for coordinate geometry applications and graphical programming.

Updated on: 2026-03-15T14:05:44+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements