C Program to accept a coordinate and find its quadrant


February 26, 2022, Learn eTutorial
1372

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

Here in this c program, we need to calculate the quadrant of a point, which means we have to find out where quadrant a point is coming from.

What are the different quadrants?

There are mainly four Quadrants.

  1. First Quadrant: Here, both the points 'x' and 'y' take the Positive value. That is 'x>0' and 'y>0'.
  2. Second Quadrant: Here, the value of 'x' is Positive, and 'y' is Negative. That is 'x>0' and 'y<0'.
  3. Third Quadrant: Here, both the points 'x' and 'y' take the Negative value. That is 'x<0' and 'y<0'.
  4. Fourth Quadrant: Here, the point 'x' is Positive, and 'y' takes the Negative value. That is 'x>0' and 'y<0'.
  5. Origin: Here the value of both 'x' and 'y' is Zero.That is 'x=0','y=0'.

There are four quadrants, namely the first, second, third, and fourth quadrants. The logic of the program is first to declare the variables 'x' and 'y' then read the values for 'x' and 'y' from the user. By using the if-else statement check, we have to check if 'x' and 'y' are greater than zero. If so, the point is in the first quadrant. If check 'x' is less than zero and 'y' is greater than zero, then the point is in the second quadrant. Else if 'x' greater than zero and 'y' is less than zero, then it is the fourth quadrant. Else if 'x' and 'y' less than zero, then it is in the third quadrant. Finally, if 'x' and 'y' equal zero, then the point is in origin.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the integer variable x, y.

STEP 3: Read the values of X and Y into the variable x, y.

STEP 4: Check if x>0 and y>0; if the condition is true, then Display x and y lies the First Quadrant.

STEP 5: Else check if x<0 and y>0, if the condition is true, then display x and y lies in the Second Quadrant.

STEP 6: Else check if x>0 and y<0; if the condition is true, then display x and y lies in the Third Quadrant.

STEP 7: Check if x>0 and y<0; if the condition is true, then display x and y lies the Fourth Quadrant.

STEP 8: Check if x=0 and y=0, if the condition is true, then display x and y lies at the origin.

C Source Code

                                          #include <stdio.h>

void main()
{
  int x, y;
  printf("Enter the values for X and Y\n");
  scanf("%d %d", & x, & y);
  if (x > 0 && y > 0)
    printf("point (%d,%d) lies in the First quadrant\n",x,y);
  else if (x < 0 && y > 0)
    printf("point (%d,%d) lies in the Second quadrant\n",x,y);
  else if (x < 0 && y < 0)
    printf("point (%d, %d) lies in the Third quadrant\n",x,y);
  else if (x > 0 && y < 0)
    printf("point (%d,%d) lies in the Fourth quadrant\n",x,y);
  else if (x == 0 && y == 0)
    printf("point (%d,%d) lies at the origin\n",x,y);
} /* End of main() */ /* End of main() */
                                      

OUTPUT

RUN 1

Enter the values for X and Y
3 5
point (5,3) lies in the First quadrant

RUN 2

Enter the values for X and Y
-4
-7
point (-7, -4) lies in the Third quadrant