polar() function for complex number in C++
In this tutorial, we are going to learn about polar() function for complex number in C++. Here we will learn about polar() function uses, and its header file. After that, we will see the C++ program for the polar function with several examples.
polar() function
The polar() function is defined in the #include<complex> header file in c++. It takes the magnitude and phase angle of a complex number and construct a complex number using these values. As we know,
real = magnitude*cosine(phase angle)
imaginary = magnitude*sine(phase angle)
The general syntax of the polar function is:-
polar(mag,angle);
where mag represents the magnitude and angle represents the phase angle.
Let us see the code for polar function:-
#include<iostream>
#include<complex>
using namespace std;
/*===========================================
MAIN FUNCTION
=============================================*/
int main ()
{
cout<<"Example 1"<<endl;
float m=1.2,a=1.5;
cout<<"Complex number with magnitude "<<m<<" and phase angle "
<<a<<" is ";
cout<<polar(m,a)<<endl;
cout<<"Example 2"<<endl;
m=2.0,a=3.1;
cout<<"Complex number with magnitude "<<m<<" and phase angle "
<<a<<" is ";
cout<<polar(m,a)<<endl;
cout<<"Example 3"<<endl;
m=2.2,a=1.8;
cout<<"Complex number with magnitude "<<m<<" and phase angle "
<<a<<" is ";
cout<<polar(m,a)<<endl;
cout<<"Example 4"<<endl;
m=3.2,a=1.4;
cout<<"Complex number with magnitude "<<m<<" and phase angle "
<<a<<" is ";
cout<<polar(m,a)<<endl;
cout<<"Example 5"<<endl;
m=5.0,a=2.0;
cout<<"Complex number with magnitude "<<m<<" and phase angle "
<<a<<" is ";
cout<<polar(m,a)<<endl;
return 0;
}Output:-
Example 1 Complex number with magnitude 1.2 and phase angle 1.5 is (0.0848846,1.19699) Example 2 Complex number with magnitude 2 and phase angle 3.1 is (-1.99827,0.0831615) Example 3 Complex number with magnitude 2.2 and phase angle 1.8 is (-0.499844,2.14246) Example 4 Complex number with magnitude 3.2 and phase angle 1.4 is (0.543895,3.15344) Example 5 Complex number with magnitude 5 and phase angle 2 is (-2.08073,4.54649)
Thanks for reading this tutorial. I hope it helps you !!
Leave a Reply