LearnFrenzy
Sign in Take a free test

Decision Making · Decision Making Programs · Question 4

Quadrant of a point (or which axis it lies on) in C Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

Report the quadrant of a point, or whether it lies on an axis or at the origin.

Solution
main.c
#include <stdio.h>

const char *where(int x, int y) {
    if (x == 0 && y == 0) return "origin";
    if (x == 0)           return "on the y-axis";
    if (y == 0)           return "on the x-axis";
    if (x > 0)            return y > 0 ? "quadrant I" : "quadrant IV";
    return y > 0 ? "quadrant II" : "quadrant III";
}

int main(void) {
    int p[][2] = {{3, 4}, {-2, 5}, {-1, -1}, {4, -7}, {0, 3}, {5, 0}, {0, 0}};
    for (int i = 0; i < 7; i++)
        printf("(%d, %d): %s\n", p[i][0], p[i][1], where(p[i][0], p[i][1]));
    return 0;
}

Output Verified by compiling and running

(3, 4): quadrant I
(-2, 5): quadrant II
(-1, -1): quadrant III
(4, -7): quadrant IV
(0, 3): on the y-axis
(5, 0): on the x-axis
(0, 0): origin

The special cases (origin, axes) are handled first, so the remaining tests only need the signs. The most specific case – both zero – must come before the single-axis checks.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links