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

x y II I III IV (-,+) (+,+) (-,-) (+,-)

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 && y 

Example

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 && y 

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) ? "Quadrant II" : 
             (x  0 && y 

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.

Updated on: 2026-03-17T07:04:36+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements