Return Quadrants for Given Coordinates in C++



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 > 0 and >y > 0, it lies in Quadrant I.
  • If >x < 0 and >y > 0, it lies in Quadrant II.
  • If >x < 0 and >y < 0, it lies in Quadrant III.
  • If >x > 0 and >y < 0, it lies in Quadrant IV.
  • If >x == 0 and >y != 0, it lies on the y-axis.
  • If >x != 0 and >y == 0, it lies on the x-axis.
  • If >x == 0 and >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

Updated on: 2024-11-28T11:31:47+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements