Program to Determine Quadrants of a Given Coordinate in C

Quadrants of a Coordinate

A coordinate plane is divided into four quadrants:

  • Quadrant 1: (+x, +y)
  • Quadrant 2: (-x, +y)
  • Quadrant 3: (-x, -y)
  • Quadrant 4: (+x, -y)

We will explore three methods to determine the quadrant of a given coordinate using C programming.

Method 1: Using If-Else Statements

We use simple if-else conditions to check the signs of x and y.

#include <stdio.h>

void findQuadrant(int x, int y) {
    if (x > 0 && y > 0)
        printf("Quadrant 1\n");
    else if (x < 0 && y > 0)
        printf("Quadrant 2\n");
    else if (x < 0 && y < 0)
        printf("Quadrant 3\n");
    else if (x > 0 && y < 0)
        printf("Quadrant 4\n");
    else
        printf("Point lies on the axis\n");
}

int main() {
    int x, y;
    printf("Enter coordinates (x, y): ");
    scanf("%d %d", &x, &y);
    findQuadrant(x, y);
    return 0;
}
            

Example Output:

Enter coordinates (x, y): 5 7
Quadrant 1
            

Method 2: Using Switch Case

We map the signs of x and y to a switch case structure.

#include <stdio.h>


void findQuadrant(int x, int y) {
    switch ((x > 0) * 2 + (y > 0)) {
        case 3: printf("Quadrant 1\n"); break;
        case 1: printf("Quadrant 2\n"); break;
        case 0: printf("Quadrant 3\n"); break;
        case 2: printf("Quadrant 4\n"); break;
        default: printf("Point lies on the axis\n");
    }
}

int main() {
    int x, y;
    printf("Enter coordinates (x, y): ");
    scanf("%d %d", &x, &y);
    findQuadrant(x, y);
    return 0;
}
            

Example Output:

Enter coordinates (x, y): -3 4
Quadrant 2
            

Method 3: Using Recursion

We use a recursive function to determine the quadrant.

#include <stdio.h>

void findQuadrantRecursive(int x, int y) {
    if (x == 0 || y == 0) {
        printf("Point lies on the axis\n");
        return;
    }
    if (x > 0 && y > 0) {
        printf("Quadrant 1\n");
    } else if (x < 0 && y > 0) {
        printf("Quadrant 2\n");
    } else if (x < 0 && y < 0) {
        printf("Quadrant 3\n");
    } else {
        printf("Quadrant 4\n");
    }
}

int main() {
    int x, y;
    printf("Enter coordinates (x, y): ");
    scanf("%d %d", &x, &y);
    findQuadrantRecursive(x, y);
    return 0;
}
            

Example Output:

Enter coordinates (x, y): -6 -8
Quadrant 3
            
Numbers

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

HCF - Highest Common Factor: C C++ Java Python

LCM - Lowest Common Multiple: C C++ Java Python

GCD - Greatest Common Divisor: C C++ Java Python

Binary to Decimal Conversion: C C++ Java Python

Octal to Decimal Conversion: C C++ Java Python

Hexadecimal to Decimal Conversion: C C++ Java Python

Decimal to Binary Conversion: C C++ Java Python

Decimal to Octal Conversion: C C++ Java Python

Decimal to Hexadecimal Conversion: C C++ Java Python

Binary to Octal Conversion: C C++ Java Python

Quadrants in which a given coordinate lies: C C++ Java Python

Addition of Two Fractions: C C++ Java Python

Calculate the Area of a Circle: C C++ Java Python

Convert Digit/Number to Words: C C++ Java Python

Finding Roots of a Quadratic Equation: C C++ Java Python