
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Complex Numbers from Imaginary Parts in Go
In this tutorial we will learn how to create Complex numbers from given imaginary parts in Go programming language.
A complex number is a basic data type of GoLang and is a combination of the real and imaginary part, where both parts of the complex number will be float type in golang.
Complex Number = Real Number + Imaginary Number
The function complex()in Go programming language is a built-in function that is used to construct a complex number from two floating-point values.
The real and imaginary parts of the complex value must be of the same size and the return value will be the corresponding complex type.
The values of real and imaginary parts are the parameters to construct a complex type number.
Example: Golang Program code to Create Complex Number From the given Imaginary Parts in one Single Function
Syntax
func complex(real, imaginary FloatType) ComplexType
Algorithm
Step 1 ? Import the package fmt
Step 2 ? Start function main().
Step 3 ? Construct the complex variable using the imaginary parts using Complex().
Step 4 ? Initialize the variable using the constructor init method
Step 5 ? Print the complex number on the screen using fmt.Println().
Example
package main // fmt package provides the function to print anything import ( "fmt" ) func main() { fmt.Println("Golang Program to create Complex numbers from given imaginary parts") // Constructing complex values // initializing the variable using the constructor init // using the function func complex(r, i FloatType) ComplexType c1 := complex(7.25, 51.8) c2 := complex(10, 20) c3 := complex(-10.5, 20) c4 := complex(1, -45.5) c5 := complex(-11.45, 5) // Printing the complex numbers from given imaginary parts fmt.Println("The complex number of c1 is", c1) fmt.Println("The complex number of c2 is", c2) fmt.Println("The complex number of c3 is", c3) fmt.Println("The complex number of c4 is", c4) fmt.Println("The complex number of c15 is", c5) }
Output
Golang Program to create Complex numbers from given imaginary parts The complex number of c1 is (7.25+51.8i) The complex number of c2 is (10+20i) The complex number of c3 is (-10.5+20i) The complex number of c4 is (1-45.5i) The complex number of c15 is (-11.45+5i)
Description of the Code
In the above program, we first declare the package main.
We imported the fmt package that includes the files of package fmt which provides the function to print anything.
We start the function main().GO program execution starts with the function main().
In the next line of code we have to create the complex number from its given imaginary parts.
Next initialize the variable by using the constructor init method with their real and imaginary values and call the Complex() function.
Last print the created complex number on the screen using the built-in function fmt.Println().
Conclusion
We have successfully compiled and executed example of the Golang code on how to create Complex numbers from given imaginary parts in Go programming language.
In this example we have used an in-built function Complex() to create the complex number.