Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Java program to find out the quadrant in which the given co-ordinate lie

Quadrant in which Coordinate Lie

Quadrant in which the given co-ordinate lie

There are 2 axis (say x and y axis) in a two-dimensional planes. Based on values either positive or negative, a point is decided to lie on which co-ordinate. We need simple if-else statements to write a Java program to find out the quadrant in which the given co-ordinate lie. Generally there are four quadrants. 

  • Quadrant-1 : both x and y are positive , (+x,+y)
  • Quadrant-2 : x is negative and y is positive, (-x,+y)
  • Quadrant-3 : both x and y are negative, (-x,-y)
  • Quadrant-4 : x is positive and y is negative, (+x,-y)

To better understand see the below example,

  1. x=4  and y=5 , Quadrant 1
  2. x=-4 and y=5, Quadrant 2
  3. x=-4 and y=-5 Quadrant 3
  4. x=4 and y=-5  Quadrant 4

Algorithm

  • Step 1:- Start.
  • Step 2:- Take inputs for X and Y.
  • Step 3:- Check if X and Y are zeroes not if yes then print Origin else check if both X and y are positive Print First quadrant.
  • Step 4:- Else if check X is negative and Y is positive print Second quadrant.
  • Step 5:- Else if check X and Y both are negative print Third quadrant.
  • Step 6:- Else if check X is positive and Y is negative print Fourth quadrant.
  • Step 7:- Else print X and Y are at origin.
  • Step 8:- End.

Check below Java program to find out the quadrant in which the given co-ordinate lie using above algorithm

Java Code:

import java.io.*;
class PREPINSTA{
    public static void main (String[] args) {
        int x=-4,y=5;
        if(x==0 && y==0)
        {
         System.out.println(“Origin”);
        }
        else
        {
         if(x>0 && y>0
            System.out.println(“Quadrant I”);   
         else if(x<0 && y>0)
            System.out.println(“Quadrant II”);
         else if(x<0 && y<0)
            System.out.println(“Quadrant III”);
         else
            System.out.println(“Quadrant IV”);
        }
    }
}

Output:

Quadrant II