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 Quadrants in which a given Coordinates Lies
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.
Problem Description
We are given the position of a point (x, y), and we have to determine in which quadrant this point lies.
Example
Input: (-3, 2)
Output: Quadrant II
Input: (2, 5)
Output: Quadrant I
Input: (2, -4)
Output: Quadrant IV
Input: (-6, -3)
Output: Quadrant III
Using Conditional Quadrant Identification Approach
In this approach, we use an if-else condition to print the quadrant in which the point lies:
- If
>x > 0and>y > 0, it lies in Quadrant I. - If
>x < 0and>y > 0, it lies in Quadrant II. - If
>x < 0and>y < 0, it lies in Quadrant III. - If
>x > 0and>y < 0, it lies in Quadrant IV. - If
>x == 0and>y != 0, it lies on the y-axis. - If
>x != 0and>y == 0, it lies on the x-axis. - If
>x == 0and>y == 0, it lies on the origin.
C++ Implementation
#include<bits/stdc++.h>
using namespace std;
// Function to determine the quadrant of a point
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";
}
}
int main() {
int x = 5;
int y = -2;
cout << "The point (" << x << ", " << y << ") lies in " << findQuadrant(x, y) << endl;
return 0;
}
Output:
The point (5, -2) lies in Quadrant IV
Time Complexity: O(1), constant time
Space Complexity: O(1), constant space
Using Ternary Quadrant Evaluation Approach
We can use the ternary operator to determine the quadrant in which a given point lies. The logic is the same as the above approach. The ternary operator (>? :) evaluates conditions in a nested structure.
C++ Implementation
#include<bits/stdc++.h>
using namespace std;
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";
}
int main() {
int x = 5;
int y = -2;
cout << "The point (" << x << ", " << y << ") lies in " << findQuadrant(x, y) << endl;
return 0;
}
Output:
The point (5, -2) lies in Quadrant IV
Time Complexity: O(1), constant time
Space Complexity: O(1), constant space