Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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)
// Second quadrant
else if (x 0 && y
Quadrant Rules
- 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
", x, y);
}
else if (x < 0 && y > 0) {
printf("Point (%d, %d) lies in the 2nd Quadrant
", x, y);
}
else if (x < 0 && y < 0) {
printf("Point (%d, %d) lies in the 3rd Quadrant
", x, y);
}
else if (x > 0 && y < 0) {
printf("Point (%d, %d) lies in the 4th Quadrant
", x, y);
}
else if (x == 0 && y == 0) {
printf("Point (%d, %d) is at the Origin
", x, y);
}
else if (x == 0) {
printf("Point (%d, %d) lies on the Y-axis
", x, y);
}
else {
printf("Point (%d, %d) lies on the X-axis
", 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
");
else if (x < 0 && y > 0)
printf("2nd Quadrant
");
else if (x < 0 && y < 0)
printf("3rd Quadrant
");
else if (x > 0 && y < 0)
printf("4th Quadrant
");
else if (x == 0 && y == 0)
printf("Origin
");
else if (x == 0)
printf("Y-axis
");
else
printf("X-axis
");
}
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.
Advertisements
