![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
#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