Home  >  Article  >  Backend Development  >  C++ program to get the imaginary part of a given complex number

C++ program to get the imaginary part of a given complex number

PHPz
PHPzforward
2023-09-06 18:05:05690browse

C++ program to get the imaginary part of a given complex number

Modern science relies heavily on the concept of plural numbers, which was first introduced in the 16th century by Girolamo Cardano Founded in the early 17th century. The formula for plural numbers is a ib where a holds the html code and b is a real number. A complex number is said to have two parts: the real part and the imaginary part (). The value of i or iota is √-1. The complex number class in C is a Class used to represent complex numbers. The complex class in C can represent and controls several complex number operations. Let's take a look at how to represent and control Show plural numbers.

imag() member function

As mentioned before, complex numbers are composed of real part and imaginary part.

To display the real part we use the real() function and the imag() function to display Given the imaginary part of a complex number. In the next example, we take a complex number number object, initialize it and then display the real and imaginary parts of the number respectively.

grammar

//displaying the imaginary part only
complex<double> c;
cout << imag(c) << endl;

algorithm

  • Get a new plural object.

  • Use the 'a.ib' notation to assign a value to the object.

  • Use the imag() function to display the imaginary part of a complex value.

Example

#include <iostream>
#include <complex>
using namespace std;
//displays the imaginary part of the complex number
void display(complex <double> c){
   cout << "The imaginary part of the complex number is: ";
   cout << imag(c) << endl;
}
//initializing the complex number
complex<double> solve( double real, double img ){
   complex<double> cno(real, img);
   return cno;
}
int main(){
   complex<double> cno = 10.0 + 11i;
   //displaying the complex number
   cout << "The complex number is: " << real(cno) << '+' << imag(cno) << 'i' << endl;
   display(cno);
   return 0;
}

Output

The complex number is: 10+11i
The imaginary part of the complex number is: 11

It should be noted that the imag() function only accepts complex numbers as input, and

Returns the imaginary part of the supplied complex number. The output value is

Template parameters of plural objects.

in conclusion

For many different operations across a wide range of scientific fields, complex

numbers are required. An interface for representing complex numbers is provided by the C complex class, which is part of the header file . All complex number operations, Including addition, subtraction, multiplication, conjugate, norm, etc., Supported via plural classes. Complex numbers can be easily created using the plural class Traditional numerical values, as we just discussed in this article. The importance of maintaining these values Note that when displaying complex numbers, the for the real and imaginary parts number must both be taken into consideration. Otherwise, several issues could arise. The The programmer must ensure that no mistakes are made between and ()> imag() function or other cases, the value will be reversed.

The above is the detailed content of C++ program to get the imaginary part of a given complex number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete