The Cartesian coordinate plane is divided into four quadrants by the X and Y axes. Given a point (x, y), this program determines which quadrant it lies in — or whether it falls on an axis or at the origin. The logic is a direct translation of the mathematical definitions into C’s if-else chain.
| Region | Condition | Example |
|---|---|---|
| Origin | x=0, y=0 | (0, 0) |
| Y-axis | x=0, y≠0 | (0, 5) |
| X-axis | x≠0, y=0 | (3, 0) |
| Quadrant I | x>0, y>0 | (3, 4) |
| Quadrant II | x<0, y>0 | (−2, 5) |
| Quadrant III | x<0, y<0 | (−1, −3) |
| Quadrant IV | x>0, y<0 | (4, −2) |
C Program: Coordinate Quadrant
/* Determine the quadrant of a Cartesian coordinate point (x, y)
* Compile: gcc -ansi -Wall -Wextra quadrant.c -o quadrant */
#include <stdio.h>
int main(void)
{
int x, y;
printf("Enter x and y coordinates: ");
if (scanf("%d %d", &x, &y) != 2) {
printf("Invalid input.\n");
return 1;
}
if (x == 0 && y == 0)
printf("(%d, %d) is at the Origin.\n", x, y);
else if (x == 0)
printf("(%d, %d) is on the Y-axis.\n", x, y);
else if (y == 0)
printf("(%d, %d) is on the X-axis.\n", x, y);
else if (x > 0 && y > 0)
printf("(%d, %d) is in Quadrant I (x>0, y>0).\n", x, y);
else if (x < 0 && y > 0)
printf("(%d, %d) is in Quadrant II (x<0, y>0).\n", x, y);
else if (x < 0 && y < 0)
printf("(%d, %d) is in Quadrant III (x<0, y<0).\n", x, y);
else
printf("(%d, %d) is in Quadrant IV (x>0, y<0).\n", x, y);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra quadrant.c -o quadrant
./quadrant
Sample Output
Enter x and y coordinates: 3 4 (3, 4) is in Quadrant I (x>0, y>0). Enter x and y coordinates: -2 5 (-2, 5) is in Quadrant II (x<0, y>0). Enter x and y coordinates: 0 0 (0, 0) is at the Origin. Enter x and y coordinates: 5 0 (5, 0) is on the X-axis.
Code Explanation
- Check the special cases first — origin (x==0, y==0) is checked before x==0 alone, because the origin would also match “x==0” if that came first. Handling the more specific case (origin) before the more general one (on Y-axis) prevents incorrect output. This is the general principle: order if-else branches from most specific to most general.
- The else case is always Quadrant IV — after ruling out origin, axes, and Quadrants I–III, the only remaining case is x>0, y<0. Using a final
elsewithout a condition is correct and slightly more efficient than writingelse if (x > 0 && y < 0)— the compiler sees no additional condition to evaluate. - Integer vs floating-point coordinates — this program reads integers. For real-valued coordinates, change
int x, ytodouble x, yand usescanf("%lf %lf", &x, &y). Floating-point comparison with exactly 0.0 can be unreliable for computed coordinates — use only for directly-entered user input.
What This Program Teaches
- if-else chain ordering — each branch is checked only if all previous branches were false. Order from most specific to most general. The origin case must come before the Y-axis case because (0,0) satisfies both x==0 AND y==0 — only the first matching branch executes.
- Using the final else as a catch-all — when you have exhaustively partitioned all cases and are sure the last one covers everything remaining,
elsewithout a condition is correct, readable, and avoids repeating the logical negation of all prior cases. - Real-world use — quadrant checks appear in game physics (which direction is a joystick pointing?), image processing (which region of a frame?), navigation (which bearing sector?), and collision detection (which side of an object did the projectile hit?).
Related Programs
- Leap Year Check in C
- Odd or Even Check in C
- Quadratic Equation Roots in C
- Mean, Variance, Standard Deviation in C
- Pointers in C — Complete Guide
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.