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.

<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

  1. function Complex(real1, imaginary1)
  2.   this.real1 = 0
  3.   this.imaginary1 = 0
  4.   this.real1 = (typeof real1 === ‘undefined’) ? this.real1 : parseFloat(real1)
  5.   this.imaginary1 = (typeof imaginary1 === ‘undefined’) ? this.imaginary1 : parseFloat(imaginary1)
  6. Complex.transform = function(num)
  7.   var complex;
  8.   complex = (num instanceof Complex) ? num : complex
  9.   complex = (typeof num === ‘number’) ? new Complex(num, 0) : num
  10.   return complex
  11. function display_complex(re, im)
  12.   if(im === ‘0’) return ” + re
  13.   if(re === 0) return ” + im + ‘i’
  14.   if(im < 0) return ” + re + im + ‘i’
  15.   return ” + re + ‘+’ + im + ‘i’
  16. function complex_num_add(first, second)
  17.   var num1 num2
  18.   num1 = Complex.transform(first)
  19.   num2 = Complex.transform(second)
  20.   var real1 = num1.real1 + num2.real1
  21.   var imaginary1 = num1.imaginary1 + num2.imaginary1
  22.   return display_complex(real1, imaginary1)
  23.  var a = new Complex(4, -5)
  24.  var b = new Complex(4,  3)
  25. 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

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.