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) ? Quadrant II if (x 0 && yExample
using System; class Program { static string FindQuadrant(int x, int y) { if (x > 0 && y > 0) { return "Quadrant I"; } else if (x 0) { return "Quadrant II"; } else if (x 0 && yThe 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 originUsing 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) ? "Quadrant II" : (x 0 && yThe 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 originComparison
| 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.
