- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to Add Two Numbers in Golang?
- Golang Program to Add Two Complex Numbers by Passing Class to a Function
- Program to Add Two Complex Numbers in C
- Java Program to Add Two Complex numbers
- Haskell program to add two complex numbers
- Kotlin Program to Add Two Complex numbers
- Write a program to add two complex numbers using C
- C++ program to overload addition operator to add two complex numbers
- How to add two complex numbers by passing structure to a function in C language?
- How to Swap Two Numbers in Golang?
- How to add Two Binary Strings in Golang?
- How to Multiply Two Floating-Point Numbers in Golang?
- Golang Program To Add Two Matrices
- How to Add two Numbers in Swift Program?
- How to Plot Complex Numbers in Python?
