इस लेख में, हम समझेंगे कि जावा में दो सम्मिश्र संख्याओं को कैसे जोड़ा जाता है। उनके पास 'मैं' है, जो इससे जुड़ा एक काल्पनिक हिस्सा है।
नीचे उसी का एक प्रदर्शन है -
इनपुट
मान लीजिए हमारा इनपुट है -
15 +i24 and 3 +i7
आउटपुट
वांछित आउटपुट होगा -
18 +i31
एल्गोरिदम
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
उदाहरण 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); } }
आउटपुट
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
उदाहरण 2
यहां, पूर्णांक को पहले परिभाषित किया गया है, और इसके मान को एक्सेस किया जाता है और कंसोल पर प्रदर्शित किया जाता है।
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(); } }
आउटपुट
The two complex numbwes are defined as 15 +i24 And 3 +i7 The sum of the two complex numbers is : 18 i31