From 98853e43a55ec18a43528f2a0d53b4c92245bf2f Mon Sep 17 00:00:00 2001 From: Arghya Ghosh <71373838+uiuxarghya@users.noreply.github.com> Date: Thu, 13 Oct 2022 00:08:22 +0530 Subject: [PATCH 1/2] Create java-program-to-add-two-complex-numbers.mdx --- ...ava-program-to-add-two-complex-numbers.mdx | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/pages/programs/java-program-to-add-two-complex-numbers.mdx diff --git a/src/pages/programs/java-program-to-add-two-complex-numbers.mdx b/src/pages/programs/java-program-to-add-two-complex-numbers.mdx new file mode 100644 index 00000000..d60abcf3 --- /dev/null +++ b/src/pages/programs/java-program-to-add-two-complex-numbers.mdx @@ -0,0 +1,54 @@ +--- +title: Java Program to Add Two Complex Numbers +shortTitle: Add Two Complex Numbers +description: In this tutorial, we will write a Java program to add two complex numbers. +--- + +Complex numbers have two parts – real part and imaginary part. When adding complex numbers we add real parts together and imaginary parts together as shown in the following diagram. + + +## Adding two complex numbers in Java + +n this program we have a class `ComplexNumber`. In this class we have two instance variables `real` and `img` to hold the real and imaginary parts of complex numbers. + +We have declared a method sum() to [add the two numbers](./add-two-integers) by adding their real and imaginary parts together. + +The constructor of this class is used for initializing the complex numbers. For e.g. when we create an instance of this class like this `ComplexNumber temp = new ComplexNumber(0, 0);`, it actually creates a complex number `0 + 0i`. + +### Code: + +```java +public class ComplexNumber{ + //for real and imaginary parts of complex numbers + double real, img; + + //constructor to initialize the complex number + ComplexNumber(double r, double i){ + this.real = r; + this.img = i; + } + + public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) + { + //creating a temporary complex number to hold the sum of two numbers + ComplexNumber temp = new ComplexNumber(0, 0); + + temp.real = c1.real + c2.real; + temp.img = c1.img + c2.img; + + //returning the output complex number + return temp; + } + public static void main(String args[]) { + ComplexNumber c1 = new ComplexNumber(5.5, 4); + ComplexNumber c2 = new ComplexNumber(1.2, 3.5); + ComplexNumber temp = sum(c1, c2); + System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i"); + } +} +``` +### Output: + +```text +Sum is: 6.7 + 7.5i +``` From 679d25a13b474ccd9c82ddaada28196b37bb9cea Mon Sep 17 00:00:00 2001 From: Arghya Ghosh <71373838+uiuxarghya@users.noreply.github.com> Date: Thu, 13 Oct 2022 00:08:34 +0530 Subject: [PATCH 2/2] Update program.js --- src/navs/program.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/navs/program.js b/src/navs/program.js index 758a84f5..b17f5d62 100644 --- a/src/navs/program.js +++ b/src/navs/program.js @@ -12,5 +12,6 @@ export const programsNav = { pages['add-two-integers'], pages['check-even-or-odd'], pages['java-program-to-add-two-binary-numbers'], + pages['java-program-to-add-two-complex-numbers'], ], }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: