imag() function in C++


In this article, we will be discussing the working, syntax, and examples of imag() function in C++.

What is imag()?

imag() function is an inbuilt function in C++ STL, which is defined in <complex>header file. imag() is used to find the imaginary part of the complex number.

A complex number is a number which is made by the combination of a real number and an imaginary number. Real numbers are any number except the infinity and imaginary numbers.

Imaginary numbers are those numbers whose square is a negative number. The function returns the imaginary part, the imaginary part which is the factor by which the the imaginary unit is multiplied.

Syntax

Template <class T> T imag(const complex<T>& num);

Parameters

The function accepts the following parameter(s) −

  • num − This is the given complex number.

Return value

This function returns the imaginary part of the num.

Input 

complex<double> img(2.2,3.4);
imag(img);

Output 

3.4

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //complex number = (a + ib)
   complex<double> img(2.2,3.4);
   cout<<"The complex number is: "<<img;
   cout<<"\nThe Imaginary part of the complex number is: "<<imag(img) << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

The complex number is: (2.2,3.4)
The Imaginary part of the complex number is: 3.4

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //complex number = (a + ib)
   complex<double> img(32,12);
   cout<<"The complex number is: "<<img;
   cout<<"\nThe Imaginary part of the complex number is: "<<imag(img) << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

The complex number is: (32,12)
The Imaginary part of the complex number is: 12

Updated on: 22-Apr-2020

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements