- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Complex Numbers in Java
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.
Example
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); } }
Output
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.
- Related Articles
- Java Program to Add Two Complex numbers
- Complex Numbers in C#
- Complex numbers in C++
- Complex Numbers in Python?
- Complex numbers in C++ programming
- Complex numbers operations in Arduino
- Geometry using Complex Numbers in C++
- Python program for Complex Numbers
- 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 to add two Complex numbers in Golang?
- Kotlin Program to Add Two Complex numbers
- Haskell program to add two complex numbers
