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
C# Program to Return Quadrant in which the Coordinate Lie
The Cartesian coordinate system is divided into four quadrants based on the signs of x and y coordinates. In this article, we will learn how to determine which quadrant a given point lies in using C#.
Understanding Quadrants
The coordinate plane is divided into four regions called quadrants
- Quadrant I: x > 0 and y > 0 (both positive)
- Quadrant II: x 0 (x negative, y positive)
- Quadrant III: x
- Quadrant IV: x > 0 and y
Problem Examples
- Input: (1, 2) ? Output: Quadrant I
- Input: (-6, 4) ? Output: Quadrant II
- Input: (-9, -2) ? Output: Quadrant III
- Input: (4, -3) ? Output: Quadrant IV
Using Conditional Statements
This approach uses if-else conditions to determine the quadrant. We also handle special cases where points lie on axes or at the origin
if (x > 0 && y > 0) ? Quadrant I if (x < 0 && y > 0) ? Quadrant II if (x < 0 && y < 0) ? Quadrant III if (x > 0 && y < 0) ? Quadrant IV if (x == 0 && y != 0) ? On y-axis if (x != 0 && y == 0) ? On x-axis if (x == 0 && y == 0) ? At origin
Example
using System;
class Program {
static string FindQuadrant(int x, int y) {
if (x > 0 && y > 0) {
return "Quadrant I";
} else if (x < 0 && y > 0) {
return "Quadrant II";
} else if (x < 0 && y < 0) {
return "Quadrant III";
} else if (x > 0 && y < 0) {
return "Quadrant IV";
} else if (x == 0 && y != 0) {
return "On the y-axis";
} else if (x != 0 && y == 0) {
return "On the x-axis";
} else {
return "At the origin";
}
}
static void Main(string[] args) {
Console.WriteLine($"Point (7, -3): {FindQuadrant(7, -3)}");
Console.WriteLine($"Point (-6, 4): {FindQuadrant(-6, 4)}");
Console.WriteLine($"Point (0, 5): {FindQuadrant(0, 5)}");
Console.WriteLine($"Point (0, 0): {FindQuadrant(0, 0)}");
}
}
The output of the above code is
Point (7, -3): Quadrant IV Point (-6, 4): Quadrant II Point (0, 5): On the y-axis Point (0, 0): At the origin
Using Ternary Operator
The ternary operator provides a more compact way to implement the same logic. Multiple ternary operators are chained together to evaluate all conditions
Example
using System;
class Program {
static string FindQuadrant(int x, int y) {
return (x > 0 && y > 0) ? "Quadrant I" :
(x < 0 && y > 0) ? "Quadrant II" :
(x < 0 && y < 0) ? "Quadrant III" :
(x > 0 && y < 0) ? "Quadrant IV" :
(x == 0 && y != 0) ? "On the y-axis" :
(x != 0 && y == 0) ? "On the x-axis" :
"At the origin";
}
static void Main(string[] args) {
int[] xValues = {1, -6, -9, 4, 0, 3, 0};
int[] yValues = {2, 4, -2, -3, 5, 0, 0};
for (int i = 0; i < xValues.Length; i++) {
Console.WriteLine($"Point ({xValues[i]}, {yValues[i]}): {FindQuadrant(xValues[i], yValues[i])}");
}
}
}
The output of the above code is
Point (1, 2): Quadrant I Point (-6, 4): Quadrant II Point (-9, -2): Quadrant III Point (4, -3): Quadrant IV Point (0, 5): On the y-axis Point (3, 0): On the x-axis Point (0, 0): At the origin
Comparison
| Approach | Readability | Time Complexity | Space Complexity |
|---|---|---|---|
| If-Else Statements | High - Easy to understand | O(1) | O(1) |
| Ternary Operator | Moderate - More compact | O(1) | O(1) |
Conclusion
Determining quadrants in C# is straightforward using coordinate sign analysis. Both if-else conditions and ternary operators provide O(1) solutions, with if-else being more readable for complex logic and ternary operators offering more compact code for simple cases.
