- 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
Java Program to Add Two Complex numbers
In this article, we will understand how to add two complex numbers in Java. They have the ‘I’ that is, an imaginary part associated with it.
Below is a demonstration of the same −
Input
Suppose our input is −
15 +i24 and 3 +i7
Output
The desired output would be −
18 +i31
Algorithm
Step1- Start Step 2- Declare three Complex numbers: my_input_1, my_input_2 and my_result Step 3- Hardcode the complex number values Step 4- Define a function add_complex_number when u add the real numbers and the imaginary numbers separately and return the result. Step 5- Store the result in my_result variable. Step 6- Display the result Step 7- Stop
Example 1
import java.util.*; class ComplexNumbers { int my_real_number, my_imaginary_number; ComplexNumbers() {} ComplexNumbers(int my_real_temp, int my_imaginary_temp){ my_real_number = my_real_temp; my_imaginary_number = my_imaginary_temp; } ComplexNumbers AddComplexNumbers(ComplexNumbers my_input_1, ComplexNumbers my_input_2){ ComplexNumbers my_temp = new ComplexNumbers(); my_temp.my_real_number = my_input_1.my_real_number + my_input_2.my_real_number; my_temp.my_imaginary_number = my_input_1.my_imaginary_number + my_input_2.my_imaginary_number; return my_temp; } } public class MainFunction { public static void main(String[] args){ System.out.println("Required packages have been imported"); ComplexNumbers my_input_1 = new ComplexNumbers(15, 24); System.out.println("The first complex number is defined as : " + my_input_1.my_real_number + " + i" + my_input_1.my_imaginary_number); ComplexNumbers my_input_2 = new ComplexNumbers(3, 7); System.out.println("The second complex number is defined as : " + my_input_2.my_real_number + " + i" + my_input_2.my_imaginary_number); ComplexNumbers my_result = new ComplexNumbers(); my_result = my_result.AddComplexNumbers(my_input_1, my_input_2); System.out.println("The sum of complex number is : " + my_result.my_real_number + " + i" + my_result.my_imaginary_number); } }
Output
Required packages have been imported The first complex number is defined as : 15 + i24 The second complex number is defined as : 3 + i7 The sum of complex number is : 18 + i31
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class ComplexNumber { int real_number, imaginary_number; public ComplexNumber(int r, int i){ this.real_number = r; this.imaginary_number = i; } public void print_complex_number(){ System.out.print(this.real_number + " +i" + this.imaginary_number); } public static ComplexNumber add_complex_number(ComplexNumber n1, ComplexNumber n2){ ComplexNumber my_result = new ComplexNumber(0, 0); my_result.real_number = n1.real_number + n2.real_number; my_result.imaginary_number = n1.imaginary_number +n2.imaginary_number; return my_result; } public static void main(String arg[]){ ComplexNumber my_input_1 = new ComplexNumber(15, 24); ComplexNumber my_input_2 = new ComplexNumber(3, 7); System.out.println("The two complex numbwes are defined as "); my_input_1.print_complex_number(); System.out.print(" and "); my_input_2.print_complex_number(); ComplexNumber my_result = add_complex_number(my_input_1, my_input_2); System.out.println("\nThe sum of the two complex numbers is :"); my_result.print_complex_number(); } }
Output
The two complex numbwes are defined as 15 +i24 And 3 +i7 The sum of the two complex numbers is : 18 i31
Advertisements