
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Return Quadrant in which the Coordinate Lie
Cartesian-coordinate System and Quadrants
The cartesian-coordinate system is divided into four Quadrants: Quadrant I, Quadrant II, Quadrant III, and Quadrant IV. In this article, we are going to learn how we can determine the quadrant in which given points i.e., x and y, lie in C#.
Problem Description
We are given the position of a point (x, y), and we have to determine in which quadrant the given point lies in C#.
Example 1
- Input: (-6, 4)
- Output: Quadrant II
Example 2
- Input: (1, 2)
- Output: Quadrant I
Example 3
- Input: (4, -3)
- Output: Quadrant IV
Example 4
- Input: (-9, -2)
- Output: Quadrant III
Using Conditional Quadrant Identification Approach
In this approach, we use if-else conditions to determine the quadrant in which given points lie. Below are the conditions to determine the quadrant in which the given point lies:
If >x > 0 and >y > 0, the given point lies in Quadrant I.
If >x < 0 and >y > 0, the given point lies in Quadrant II.
If >x < 0 and >y < 0, the given point lies in Quadrant III.
If >x > 0 and >y < 0, the given point lies in Quadrant IV.
If >x == 0 and >y != 0, the given point lies on the y-axis.
If >x != 0 and >y == 0, the given point lies on the x-axis.
If >x == 0 and >y == 0, the given point lies on the origin.
Implementation Code
using System; class Program { // Function to determine the quadrant of a point 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) { int x = 7; int y = -3; Console.WriteLine($"The point ({x}, {y}) lies in {FindQuadrant(x, y)}"); } }
Output
The point (7, -3) lies in Quadrant IV
Time Complexity: O(1), constant time
Space Complexity: O(1), constant space
Using Ternary Operator
The logic of this approach is the same as that of the above approach. We are going to use a ternary operator in this approach. In this approach, we use the ternary operator to determine the quadrant in which given points lie. The ternary operator (>? :) evaluates conditions in a nested structure.
Implementation Code
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 x = 7; int y = -3; Console.WriteLine($"The point ({x}, {y}) lies in {FindQuadrant(x, y)}"); } }
Output
The point (7, -3) lies in Quadrant IV
Time Complexity: O(1), constant time
Space Complexity: O(1), constant space