How to add two Complex numbers in Golang?


In this tutorial, we will learn how to declare and add complex numbers in Golang. Complex numbers are something that has an imaginary and real part which will make them different from other types of numbers. Golang supports declaring a variable of complex number type.

Complex Number = Real Number + Imaginary Number

Different ways to declare and initialize complex numbers and add them later in Golang.

Initializing using complex number init syntax

Syntax

var variableName complex64
var variableName complex128
variableName = (realValue) + (imaginaryValue)i

Algorithm:

  • STEP 1 − Defining the complex variables that we want to add and the complex variable in which we will add the result.

  • STEP 2 − Initialize the variables with the respective values you want to add.

  • STEP 3 − Add two numbers and store them in the third variable.

  • STEP 4 − Print the result after adding two complex numbers.

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the complex number using the var keyword var complexNumber1, complexNumber2, complexNumber3 complex64 // initializing the variable using complex number init syntax complexNumber1 = 3 + 3i complexNumber2 = 2 + 5i fmt.Println("The First Complex Number is", complexNumber1) fmt.Println("The Second Complex Number is", complexNumber2) // adding the complex numbers using + operator complexNumber3 = complexNumber1 + complexNumber2 fmt.Println("Printing the addition of two complex numbers by initializing the variable using complex number init syntax.") // printing the complex number after addition fmt.Println(complexNumber1, "+", complexNumber2, "=", complexNumber3) }

In the above code first, we are declaring the complex numbers then we are initializing two of them using complex number init syntax with their real and imaginary values. After that, we are adding the two complex numbers and store the result in the third variable, and then print it.

Output

The First Complex Number is (3+3i)
The Second Complex Number is (2+5i)
Printing the addition of two complex numbers by initializing the variable using complex number init syntax.
(3+3i) + (2+5i) = (5+8i)

Initializing using the Constructor

In this way, we are using the constructor to initialize the complex number variable you have to just pass the real and the imaginary value.

Syntax

var variableName complex64
var variableName complex128
variableName = complex(realValue, imaginaryValue)

Example

package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the complex number using the var keyword var complexNumber1, complexNumber2, complexNumber3 complex64 // initializing the variable using the constructor complexNumber1 = complex(5, 4) complexNumber2 = complex(6, 3) fmt.Println("The First Complex Number is", complexNumber1) fmt.Println("The Second Complex Number is", complexNumber2) // adding the complex numbers using + operator complexNumber3 = complexNumber1 + complexNumber2 fmt.Println("Printing the addition of two complex numbers by initializing the variable using the constructor.") // printing the complex number after addition fmt.Println(complexNumber1, "+", complexNumber2, "=", complexNumber3) }

In the above code first, we are declaring the complex numbers then we are initializing two of them using the constructor with their real and imaginary values. After that, we are adding the two complex numbers and store the result in the third variable, and then print it.

Output

The First Complex Number is (5+4i)
The Second Complex Number is (6+3i)
Printing the addition of two complex numbers by initializing the variable using the constructor.
(5+4i) + (6+3i) = (11+7i)

These are the two ways to initialize the complex number and then add them. To learn more about Golang you can explore this tutorials.

Updated on: 26-Aug-2022

543 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements