Golang Program to Add Two Complex Numbers by Passing Class to a Function


In this article, we will write a go language program to add two complex numbers by passing a class to a function. Here we will create a function to the class that will accept the class as an argument along with its properties and we can easily calculate the sum of the provided complex numbers using it.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Then, create the structure named Complex and define in it the properties real and imaginary.

  • Step 3 − Then create a function to the complex class declared above named add(). This function accepts the Complex class which contains the real and imaginary parts of a complex number and returns the final result in the same format,

  • Step 4 − Then we need to call the main() function. Inside the main() declare two complex numbers and store values to them further print these complex numbers on the screen.

  • Step 5 − Now, call the add() function defined on the complex class and pass two complex numbers as arguments to it.

  • Step 6 − Store the result obtained by the function in a separate variable. This variable contains the result of the addition and print it on the screen using fmt.Println() function.

  • Step 7 − Now, take some other examples and repeat the process to find the sum of two complex numbers by passing the class to a function.

Example

In this example, we will write a go language program to add two complex numbers by passing a class to a function.

package main
import (
   "fmt"
)
type Complex struct {
   real float64
   imag float64
}
func (c Complex) add(c2 Complex) Complex {
   return Complex{c.real + c2.real, c.imag + c2.imag}
}
func main() {
   //Example 1
   fmt.Println("Example 1")
   c1 := Complex{1.2, 3.4}
   fmt.Println("The first complex number is:", c1)
   c2 := Complex{5.6, 7.8}
   fmt.Println("The second complex number is:", c2)
   c3 := c1.add(c2)
   fmt.Printf("The complex number obtained by adding \n(%v + %vi) and (%v + %vi) is %v + %vi\n", c1.real, c1.imag, c2.real, c2.imag, c3.real, c3.imag)
   fmt.Println()
   
   //Example 2
   fmt.Println("Example 2")
   c7 := Complex{0.0, 0.0}
   fmt.Println("The first complex number is:", c7)
   c8 := Complex{3.3, 5.5}
   fmt.Println("The second complex number is:", c8)
   c9 := c7.add(c8)
   fmt.Printf("The complex number obtained by adding \n(%v + %vi) and (%v + %vi) is %v + %vi\n", c7.real, c7.imag, c8.real, c8.imag, c9.real, c9.imag)
}

Output

Example 1
The first complex number is: {1.2 3.4}
The second complex number is: {5.6 7.8}
The complex number obtained by adding 
(1.2 + 3.4i) and (5.6 + 7.8i) is 6.8 + 11.2i

Example 2
The first complex number is: {0 0}
The second complex number is: {3.3 5.5}
The complex number obtained by adding 
(0 + 0i) and (3.3 + 5.5i) is 3.3 + 5.5i

Conclusion

We have successfully complied and executed a go language program to add two complex numbers by passing a class to a function along with the examples. The program defines a function to a class that accepts the two complex numbers as arguments and returns the result by performing the addition process.

Updated on: 16-Feb-2023

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements