Golang Program to initialize and print a complex number

Go ProgrammingProgrammingServer Side Programming

In this tutorial, we will learn how to declare and initialize a complex number 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 numbers are further categorized into two parts in go language. These two parts are: Complex64 and Complex128

Complex Number = Real Number + Imaginary Number

Initializing complex numbers is really easy. You can use the constructor or the initialization syntax as well.

c1 := complex(10, 11) // constructor init
c2 := 10 + 11i // complex number init syntax

fmt is a Go package that is used to format basic strings, values, inputs, and outputs. It can also be used to print and write from the terminal. Additionally, Go has formats called verbs. A verb is a placeholder for a named value to be formatted.

Functions in fmt package

  • fmt.Printf() − It prints the output depending on the specifier. Also, it returns the number of bytes it is printing and the error.

  • fmt.Println() − In this function, you can pass the variables and string directly without mentioning the format specifier you have to just separate them by ‘,’.

  • fmt.Scanf() function in Go language scans the input texts which is given in the standard input, reads from there and stores the successive space-separated values into successive arguments as determined by the format.

Different ways to declare and initialize a complex number and print it in Golang

Initialize a complex number using its init syntax and print it

Syntax

var variableName complex64
variableName = (realValue) + (imaginaryValue)i
Syntax for Println() = fmt.Println(...interface{})

Algorithm

Step 1 − Import the package fmt

Step 2 − Start function main()

Step 3 − Declare the complex variable

Step 4 − Initialize the variable with the respective value

Step 5 − Print the complex number on the screen using fmt.Println()

Example

In this article we are going to define a complex variable and assign the variable to it and then print it.

// Golang program to initialize and print a complex number package main // fmt package provides the function to print anything import "fmt" func main() { fmt.Println("Printing the complex number by initializing the variable using its init syntax.") // declaring the complex number using the var keyword var complexNumber complex64 // initializing the variable using complex number init syntax complexNumber = 5 + 7i // complex initializer syntax a + ib // printing the complex number after initializing it fmt.Println("Printing the Complex Number is", complexNumber) }

Output

Printing the complex number by initializing the variable using its init syntax.
Printing the Complex Number is (5+7i)

Description of code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt

  • var complexNumber complex64 − In this line of code we have declared the complex number type variable

  • Next initialize the variable by assigning a value to it using ‘ = ’

  • Last print the initialized complex number on the screen using fmt.Println()

Initialize a complex number using the Constructor and print it

Syntax

var variableName complex64
variableName = complex(realValue, imaginaryValue)
Syntax for Println() = fmt.Println(...interface{})

Algorithm

Step 1 − Import the package fmt

Step 2 − Start function main()

Step 3 − Declare the complex variable

Step 4 − Initialize the variable using the constructor with their real and imaginary values.

Step 5 − Print the complex number on the screen using fmt.Println()

Example

In this example we are going to use complex inbuilt function and then print the complex number.

// Golang program to initialize and print a complex number package main // fmt package provides the function to print anything import "fmt" func main() { fmt.Println("Printing the complex number by initializing the variable using the constructor.") // declaring the complex number using the var keyword var num1 complex64 // initializing the variable using the constructor num1 = complex(8, 5) // constructor init // printing the complex number after initialization fmt.Println("The initialized Complex Number is", num1) }

Output

Printing the complex number by initializing the variable using the constructor.
The initialized Complex Number is (8+5i)

Description of code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt

  • var num1 complex64 − In this line of code we have declared the complex number type variable

  • Next initialize the variable by using the constructor with their real and imaginary values

  • Last print the initialized complex number on the screen using fmt.Println()

Conclusion

We have successfully compiled and executed 2 examples of the Golang code on how to declare and initialize a complex number in Go Programming language

raja
Updated on 11-Oct-2022 07:31:50

Advertisements