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.

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

Latest posts by Prof. Fazal Rehman Shamil (see all)

Buy advertisement space on T4Tutorials

For more details email [email protected]