- Related Questions & Answers
- Complex Numbers in C#
- Complex numbers in C++
- Complex Numbers in Python?
- Complex numbers in C++ programming
- Python program for Complex Numbers
- Geometry using Complex Numbers in C++
- How to Plot Complex Numbers in Python?
- Proj() function for Complex Numbers in C++
- How can we use complex numbers in Python?
- Program to Add Two Complex Numbers in C
- How does modulus work with complex numbers in Python?
- How to find Square root of complex numbers in Python?
- How can we unpack a string of integers to complex numbers in Python?
- C++ Program to Add Complex Numbers by Passing Structure to a Function
- Complex Number Multiplication in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Complex numbers are those that have an imaginary part and a real part associated with it. They can be added and subtracted like regular numbers. The real parts and imaginary parts are respectively added or subtracted or even multiplied and divided.

public class Demo{ double my_real; double my_imag; public Demo(double my_real, double my_imag){ this.my_real = my_real; this.my_imag = my_imag; } public static void main(String[] args){ Demo n1 = new Demo(76.8, 24.0), n2 = new Demo(65.9, 11.23), temp; temp = add(n1, n2); System.out.printf("The sum of two complex numbers is %.1f + %.1fi", temp.my_real, temp.my_imag); } public static Demo add(Demo n1, Demo n2){ Demo temp = new Demo(0.0, 0.0); temp.my_real = n1.my_real + n2.my_real; temp.my_imag = n1.my_imag + n2.my_imag; return(temp); } }

The sum of two complex numbers is 142.7 + 35.2i

A class named Demo defines two double valued numbers, my_real, and my_imag. A constructor is defined, that takes these two values. In the main function, an instance of the Demo class is created, and the elements are added using the ‘add’ function and assigned to a temporary object (it is created in the main function).

Next, they are displayed on the console. In the main function, another temporary instance is created, and the real parts and imaginary parts of the complex numbers are added respectively, and this temporary object is returned as output.

Advertisements