Add two Complex Numbers in JavaScript
Add two Complex Numbers in JavaScript
JavaScript Program to Add Two Complex Numbers is today topic of discussion in our tutorial. Now, let’s start with basic concepts.
JavaScript Program to Add Two Complex Numbers
Let’s see a JavaScript Program to Add Two Complex Numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<html> <head> <meta charset="utf-8"> <title>JavaScript function to add two complex numbers</title> </head> <body> <h1>output of two complex number:</h1> <h4 id="add"></h4> </body> <script> function Complex(real1, imaginary1) { this.real1 = 0; this.imaginary1 = 0; this.real1 = (typeof real1 === 'undefined') ? this.real1 : parseFloat(real1); this.imaginary1 = (typeof imaginary1 === 'undefined') ? this.imaginary1 : parseFloat(imaginary1); } Complex.transform = function(num) { var complex; complex = (num instanceof Complex) ? num : complex; complex = (typeof num === 'number') ? new Complex(num, 0) : num; return complex; }; function display_complex(re, im) { if(im === '0') return '' + re; if
(re === 0) return '' + im + 'i'; if(im < 0) return '' + re + im + 'i'; return '' + re + '+' + im + 'i'; } function complex_num_add(first, second) { var num1, num2; num1 = Complex.transform(first); num2 = Complex.transform(second); var real1 = num1.real1 + num2.real1; var imaginary1 = num1.imaginary1 + num2.imaginary1; return display_complex(real1, imaginary1); } var a = new Complex(4, 5); var b = new Complex(4, 2); console.log(complex_num_add(a,b)); document.getElementById('add').innerHTML=complex_num_add(a,b); </script> </html> |
Output
The output of two complex numbers:
8+7!
Pseudocode of Program to Add Two Complex Numbers
- function Complex(real1, imaginary1)
- this.real1 = 0
- this.imaginary1 = 0
- this.real1 = (typeof real1 === ‘undefined’) ? this.real1 : parseFloat(real1)
- this.imaginary1 = (typeof imaginary1 === ‘undefined’) ? this.imaginary1 : parseFloat(imaginary1)
- Complex.transform = function(num)
- var complex;
- complex = (num instanceof Complex) ? num : complex
- complex = (typeof num === ‘number’) ? new Complex(num, 0) : num
- return complex
- function display_complex(re, im)
- if(im === ‘0’) return ” + re
- if(re === 0) return ” + im + ‘i’
- if(im < 0) return ” + re + im + ‘i’
- return ” + re + ‘+’ + im + ‘i’
- function complex_num_add(first, second)
- var num1 num2
- num1 = Complex.transform(first)
- num2 = Complex.transform(second)
- var real1 = num1.real1 + num2.real1
- var imaginary1 = num1.imaginary1 + num2.imaginary1
- return display_complex(real1, imaginary1)
- var a = new Complex(4, -5)
- var b = new Complex(4, 3)
- console.log(complex_num_add(a,b))
Algorithm of Program to Add two Complex Numbers
Step1: Start
Step 2: Declare variable
Step 3:Assign values to variable
Step 4:Declare a function complex
Step 5:Defining function with addition
Step 6:Function call
Step 7:Display sum
Step 8:End