Convert cartesian to polar coordinates in C++

Hello Learners, today we are going to learn how to convert Cartesian coordinates to polar coordinates in  C++. Before jumping into the coding everyone must know what is meant by Cartesian coordinates, Polar coordinates and what is use of converting. In this tutorial, we will have a detailed discussion on those topics with the code.

Cartesian coordinates can be used to pinpoint where we are on a map or graph. Polar coordinates are a set of values that quantify the location of a point based on: the distance between the point and a fixed origin and second is the angle between the point and a fixed direction.

C++: Program for the conversion of cartesian to polar coordinates

The program is very easy, before looking into the program we must the two basic formulas for the conversion: 1) r=√ (x²+y²)

2) θ= tan-¹(y/x)

were, coordinates of a point is (x,y)

r= distance from the x and y-axis.

θ= angle.

The obtained ‘θ’ will be in the from of radians we need to convert the radians into degrees. The formula used for the conversion radians to the degree is θ*180/Π. And the header file used for mathematical calculation is #include<math.h>.

code:

#include<IOSTREAM.h>
#include<conio.h>
#include<math.h>
int x,y;
float r,theta,angle;
void radius();
void theeta();
void main()
{
 clrscr();
 cout<<"enter the x coordinate:\n";
 cin>>x;
 cout<<"enter the y coordinate:\n";
 cin>>y;
radius();
theeta();
getch();
}
void radius()
{
 r=sqrt(x*x+y*y);
 cout<<"radius="<<r<<endl;
}

void theeta()
{
 theta= atan(y/x);
 angle=theta*(180/M_PI);
 cout<<"theta="<<angle;
}

Output:

enter the x coordinate:

1

enter the y coordinate:

1

radius=1.14121356

theta=45

Also, read: Convert Polar to Cartesian in C++

One response to “Convert cartesian to polar coordinates in C++”

  1. AKASH RANJAN - says:

    Thank you, waiting eagerly for data structure and algorithm also.

Leave a Reply

Your email address will not be published. Required fields are marked *