C++ program to find the Quadrant in which Coordinates lie

Quadrant in which Coordinates lie in C++

 

Here, we will discuss the problem quadrant in which coordinates lie in C++.

  • If both the coordinates are positive then the coordinates lie in the 1st quadrant
  • if x coordinate is negative and the y coordinate is positive then the coordinates lie in the 2nd quadrant. 
  • If both are negative then the coordinates lie in the 3rd quadrant.
  • if y coordinate is negative and the x coordinate is positive then the coordinates lie in the 4th quadrant.
  • If the x coordinate is 0 then the coordinates lie on the y axis.
  • If the y coordinate is 0 then the coordinates lie on the x axis.
  • If both are zero then they lie on the origin.

To find the quadrants we will be using if else statements.

Quadrant in which coordinate lie
Competitive Coding Techniques

Algorithm:-

  1. User gives two inputs(x & y coordinates).
  2. The inputs are stored in two int type variables say x & y.
  3. If x is equal to 0 then the coordinates lie on the y axis.

             if(x==0)

      4. If y is equal to 0 then the coordinates lie on the x axis.

            else if(y==0)

       5. If x & y are positive then the coordinates lie in the 1st quadrant.

            else if(x>0&&y>0)

       6. If x is negative and y is positive then the coordinates lie in the 2nd quadrant.

            else if(x<0&&y>0)

       7. If x & y are negative then the coordinates lie in the 3rd quadrant.

            else if(x<0&&y<0)

       8. If x is positive and y is positive then the coordinates lie in the 4th quadrant.

            else if(x>0&&y<0)

       9. Otherwise the coordinate lies on the origin.

Quadrant in which coordinate lie in C++

C++ Code:-

    //C++ program

//Quadrants in which coordinates lie

   #include<iostream>

    using namespace std;

    //main program

    int main()

    {

        int x, y;

        cout<<"Enter coordinates: \n";

        cin>>x>>y;

        //checking for quadrants and axis

        if(x==0)

            cout<<x<<","<<y<<" lies on y axis";

        else if(y==0)

            cout<<x<<","<<y<<" lies on x axis";

        else if(x>0&&y>0)

            cout<<x<<","<<y<<" lies in 1st quadrant";

        else if(x<0&&y>0)

            cout<<x<<","<<y<<" lies in 2nd quadrant";

        else if(x<0&&y<0)

            cout<<x<<","<<y<<" lies in 3rd quadrant";

        else if(x>0&&y<0)

            cout<<x<<","<<y<<" lies in 4th quadrant";

        else

            cout<<x<<","<<y<<" lies on the origin";

        return 0;   

    }
Output

Enter coordinates: 

1

-8

1,-8 lies in 4th quadrant
coding (3)