## c++ display complex number with i in imaginary part

c++ program to add two complex numbers using function

complex numbers in c programming language

print complex number in c++

how to print complex numbers in c++

parsing complex numbers c++

initialize complex number c++

conjugate of complex number in c++

I am doing something like:

int real_part, imaginary_part; cout<<"Please enter realpart and imaginary part"<<endl; cin>>real_part; cin>>imaginary_part; complex<double> mycomplex (real_part, imaginary_part); cout<<mycomplex<<endl; // I want to display like -2+5i but I get (-2, 5)

I am very new to c++

How can I display with `i`

like `-2+5i`

? Or I have to add `i`

char with imagginary part ?

You can use `std::real()`

and `std::imag()`

to format as you like, see complex here.

Of course, you will have to check for sign yourself.

Something like this:

std::cout << std::real(mycomplex) << (std::imag(mycomplex) >= 0.0 ? "+" : "") << std::imag(mycomplex) << " i" << std::endl;

**Complex numbers in C++,** imag() – It returns the imaginary part of the complex number. filter_none. edit close. play_arrow. link brightness_4 code cimag(z1): get the imaginary part (for float cimagf(z1), for long double cimagl(z1)) Another important point to remember when working with complex numbers is that functions like cos(), exp() and sqrt() must be replaced with their complex forms, e.g. ccos(), cexp(), csqrt().

You can simply write :

cout<< mycomplex.real << std::showpos << mycomplex.imag << "i" << endl;

**Complex number arithmetic,** Imaginary Numbers when squared give a negative result. So, a Complex Number has a real part and an imaginary part. (a+bi) + (c+di) = (a+c) + (b+d)i We can create complex number class in C++, that can hold the real and imaginary part of the complex number as member elements. There will be some member functions that are used to handle this class. In this example we are creating one complex type class, a function to display the complex number into correct format.

For the sake of completeness with the other answer. You can use `std::showpos`

to more easily format the output into something signed

cout << real(mycomplex) << std::showpos << imag(mycomplex) << "i";

**Complex Numbers,** In this C programming example, you will learn to take two complex numbers as printf("For 1st complex number \n"); printf("Enter the real and imaginary parts: A number of the form a + ib, in which i 2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. The addition and multiplication of complex numbers are defined by the following rules: (a + ib) + (c + id) = (a + c) + i (b + d ) (a + ib) * (c + id) = (ac - bd) + i (ad + bc)

If you're feeling really sneaky, or are using libraries that you dont want to or cant modify everywhere (ie I use this to print Eigen matrices in an octave / matlab compatible format), you can specialize the put-to operator for your type before including `<complex>`

. I suspect that this is against the standard, because it mucks around in `std::`

, but it works in g++(7.3.1) and clang++(5.0):

/* * this stuff can go in a header to make std::complex<> available */ typedef double real_t; #include <iostream> #define SPECIALIZED_COMPLEX_PUTTO #ifdef SPECIALIZED_COMPLEX_PUTTO #ifdef _LIBCPP_BEGIN_NAMESPACE_STD // for clang++ libs _LIBCPP_BEGIN_NAMESPACE_STD #else namespace std { #endif template <typename T> class complex; template <class T, class CharT, class Traits> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::complex<T>& x); // specialization for real_t, instantiate later template<> basic_ostream<char>& operator<<(basic_ostream<char> & o, const complex<real_t> & x); #ifdef _LIBCPP_END_NAMESPACE_STD // for clang++ _LIBCPP_END_NAMESPACE_STD #else } #endif #include <complex> /* * below here can go in a .cpp file */ #ifdef SPECIALIZED_COMPLEX_PUTTO #ifdef _LIBCPP_BEGIN_NAMESPACE_STD // for clang++ libs _LIBCPP_BEGIN_NAMESPACE_STD #else namespace std { #endif template<> basic_ostream<char>& operator<<(basic_ostream<char> & o, const complex<real_t> & x) { basic_ostringstream<char> s; s.flags(o.flags()); s.imbue(o.getloc()); s.precision(o.precision()); s << x.real() << std::showpos << x.imag() << 'i'; return o << s.str(); } #ifdef _LIBCPP_END_NAMESPACE_STD // for clang++ _LIBCPP_END_NAMESPACE_STD #else } #endif int main(int argc, char * argv[]) { std::complex<real_t> x(1.1,-2.2); std::cout << x << "\n"; }

outputs

1.1-2.2i

alternately, an example `main()`

for the Eigen case:

#include <Eigen/Dense> int main(int argc, char * argv[]) { Eigen::Matrix<std::complex<real_t>, 3,3> x; Eigen::IOFormat OctaveFmt(Eigen::StreamPrecision, 0, ", ", ";\n", "", "", "[", "]"); //srand((unsigned int) time(0)); x.setRandom(); std::cout << x.format(OctaveFmt) << "\n"; }

outputs a matrix in a format suitable for copy/paste into octave/matlab:

[ 0.680375-0.211234i, -0.329554+0.536459i, -0.270431+0.0268018i; 0.566198+0.59688i, -0.444451+0.10794i, 0.904459+0.83239i; 0.823295-0.604897i, -0.0452059+0.257742i, 0.271423+0.434594i]

Edit: added macros for apple clang libs.

**C Program to Add Two Complex Numbers by Passing Structure to a ,** We have to input the real and imaginary parts of two complex numbers separately and add them using classes and objects in C++. Expected Input and Output cin>>real;; cin>>imag;; }; /* Function to display the sum of two complex numbers */ For 1st complex number Enter the real and imaginary parts: 2.1 -2.3 For 2nd complex number Enter the real and imaginary parts: 5.6 23.2 Sum = 7.7 + 20.9i. In this program, a structure named complex is declared. It has two members: real and imag. We then created two variables n1 and n2 from this structure.

**C++ Program to Implement Complex Numbers using Classes ,** The complex number system objectives: The imaginary unit i = sqrt(-1) where i2 = -1 a and b are real numbers,a is called the real part and bi is called the imaginary part. Two complex numbers a + bi and c + di are equal iff a = c and b = d. The graph which depicts a complex number with i as the imaginary part is: Graph: D. Step-by-step explanation: We know that a complex number is represented as: z= x+iy. where x denotes the real value of the number and y denotes the imaginary value of a number. which is located as the point (x,y)

**Complex Numbers: objects and operator overloading,** Example: Program to add two complex numbers entered by. This program takes two complex numbers (entered by user) as input and displays the sum of them. //getting the value of first complex number from user cout << "Enter real and imaginary parts of first complex number:"<<endl; cin > C Programs · Java Programs The real part of a complex number z = x + iy is x, and its imaginary part is y. Based on the concept of real numbers, a complex number is a number of the form a + bi, where a and b are real numbers and i is an indeterminate satisfying i2 = −1. For example, 2 + 3i is a complex number.

**C++ Program to Add Complex Numbers,** A number of the form a + ib, in which i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. To easily handle a complex number a structure named complex has been used, which consists of two integers, first integer is for real part of a complex number and second is for imaginary part. C programming code

##### Comments

- If you do not like the built in formatting you are going to have to write your own.
- @UKMonkey,
`std::showpos`

may be a simpler approach :) - @UKMonkey, io manipulators are hardly the Atlantic. One doesn't need to understand how to implement one, in order to use one. But suit yourself.
- @StoryTeller you're right - I stand corrected.
- I would avoid using
`std::endl`

and instead use`"\n"`

or`'\n'`

unless you really know you need to flush the buffer. Otherwise you are paying for something you may not need. - Yeah, I now that. And I do. But thats what was in the OP question. I already added
`std::`

, didn't want to sound too pedantic ! ;) - There's no shame in a pedantic comment, if it endows someone with knowledge of best practices :)
- a less kludge-y way to achieve the same: gist.github.com/tesch1/50fa836e72153113fe56b14a61c8a4b6