Golang Program to get the imaginary part of the given complex number

Go ProgrammingProgrammingServer Side Programming

Cyber Security : Go from Zero to Hero

Most Popular

60 Lectures 5.5 hours

Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

In this tutorial, we are going to learn about how to get imaginary part of the complex number in Go programming language.

Complex Number = Real Number + Imaginary Number


In a complex number the real number multiplying ‘I’ is the imaginary part of a complex number. The imaginary part is the multiple of i. so if the letter z to stands for a complex number and z = a + bi where the real number ‘a’ is called the real part of the complex number a + bi and the real number ‘b’ is called its imaginary part.

Syntax

func imag(c ComplexType) FloatType


The imag() built-in function returns the imaginary part of the complex number of type float32.

Find the Imaginary part of a Complex number using imag() Function

Algorithm

Step 1 − Import the package fmt

Step 2 − Start function main()

Step 3 − Define and assign the variable

Step 4 − Calling the built-in function imag()

Step 5 − Print the imaginary part of the complex number using fmt.Println()

Example

In this example, we are going to find the imaginary part of the complex number using imag() function

// GOLANG PROGRAM TO FIND THE IMAGINARY PART OF A GIVEN COMPLEX NUMBER
package main

// fmt package provides the function to print anything
import (
"fmt"
)
func main() {

// defining and assigning a value to the variable
complex_num := 5 + 4i

// Calling the built-in function imag()
a := imag(complex_num)

// Print the imaginary part of the complex number
fmt.Println("Imaginary part of the given",complex_num,"complex number: ", a)

// Declare a complex64 variable
var number complex64

// Declare a float variable
var b float32

// Assign a value to the complex variable
number = 7 + 9i

// Calling the built-in function imag()
b = imag(number)

// Print the imaginary part of the complex number
fmt.Println("Imaginary part of the given",number,"complex number: ", b)
}


Output

Imaginary part of the given (5+4i) complex number: 4
Imaginary part of the given (7+9i) complex number: 9


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 which provides the function to print anything.

• We start the function main()

• Next define and initialize the complex type variable by assigning a value to it

• Then calling the built-in function imag() to find the imaginary part of the given complex number

• In the end we print the imaginary part of the complex number using fmt.Println()

Find the Imaginary part of a Complex Number along with its type

Algorithm

Step 1 − Import the package fmt

Step 2 − Start function main()

Step 3 − Define and assign the complex numbers

Step 4 − Calling the built-in function imag()

Step 5 − Print the imaginary part of the complex number using fmt.Printf()

Example

In this example we are going to find the imaginary part of a complex number along with its variable type

// GOLANG PROGRAM TO FIND THE IMAGINARY PART OF A GIVEN COMPLEX NUMBER
package main

// fmt package provides the function to print anything
import (
"fmt"
)
func main() {

// Declare and assign complex numbers
var x complex128 = complex(1, 27)
var y complex128 = complex(3, 9)
var z complex128 = complex(5, 6)

// Extracting the imaginary part and
// printing its value and type
imag_x := imag(x)
imag_y := imag(y)
imag_z := imag(z)
fmt.Println("Imaginary part of the given complex number: x : %v %T\n", imag_x, imag_x)
fmt.Println("Imaginary part of the given complex number: y : %v %T\n", imag_y, imag_y)
fmt.Println("Imaginary part of the given complex number: z : %v %T\n", imag_z, imag_z)
}


Output

Imaginary part of the given complex number: x : 27 float64
Imaginary part of the given complex number: y : 9 float64
Imaginary part of the given complex number: z : 6 float64


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 which provides the function to print anything.

• We start the function main()

• Next we define and initialize the complex numbers by assigning a value to it

• Next calling the built-in function imag() to find the imaginary part of the given complex number

• In the end we print the imaginary part of the complex number along with its type using fmt.Printf()

Conclusion

We have successfully compiled and executed 2 examples of the Golang code on how to find the imaginary parts of a given complex number in Go programming language.

Updated on 11-Oct-2022 07:43:27