# Golang Program to get the real part from a Complex number

In this article we will discuss about how to get real part from a complex number in Go language.

A complex number in mathematics is a number that can be expressed in the form of a + ib where i is called iota. The value of iota is $\mathrm{\sqrt{-1}}$ and a and b are real numbers.

The first part i.e ‘a’ is called real part of complex number and the part after iota i.e ’b’ is called imaginary part of complex number.

### Syntax

func real(number complex128) float64


The real() function is used to get the get the real part of any complex number. This function accepts a 64 or 128-bit complex number as an argument. If the complex number is made from two 32-bit numbers then its size is 64 bits and 128 bits if it is made from two 64 bit numbers. This function returns the real part as a 64 bit float number.

The source code is compiled and executed below.

## Example 1

### Algorithm

• Step 1 − Import the package fmt.

• Step 2 − Start the main function().

• Step 3 − Create and initialize the complex variable.

• Step 4 − Use the real() function to get the real part of the chosen complex number.

• Step 5 −Print the real part on the screen.

### Example

package main
import "fmt"
// fmt package allows us to print anything on the screen.
// calling the main() function
func main() {

// initializing the number variable to store the complex number.
var number complex64

// initializing the real_part variable to store real part of complex number.
var real_part float32

// assigning the complex number.
number = 9.89 + 10i

// getting the real part of the complex number.
real_part = real(number)

// printing the result.
fmt.Println( "The real part of the complex number", number, "is",real_part)
}


### Output

The real part of the complex number (9.89+10i) is 9.89


### Description of the Code

• First, we import the package fmt that allows us to print anything.

• Start the main() function.

• Initialize a complex variable and store a complex number in it.

• Getting the real part of the complex number using real() function.

• real() function in golang is a predefined function that accepts a complex number as an argument and returns the real part of that complex number as a float number.

• Store the value returned by real() to a separate variable. In this particular example we have used real_part variable to store the value of real part. Note that real_part is a 32-bit float type variable not a complex data type variable.

• Printing the result on the screen.

• To print the results we have used fmt.Println() function.

## Example 2

### Algorithm

• Step 1 − Import the package fmt.

• Step 2 − Initialize and define the complex_number() function that will contain the logic to get the real part of the complex number.

• Step 3 − start the main function().

• Step 4 − Initialize and assign values to the 32 bit float type variables.

• Step 5 − call the complex_number() function.

• Step 6 − Use complex() function to generate complex number out from the above float values.

• Step 7 − Use the real() function to get the real part of the chosen complex number.

• Step 8 − Print the result on the screen.

### Example

package main
import "fmt"
// fmt package allows us to print anything on the screen.
// creating the complex function
func complex_number (number1, number2 float32) {

// combining the real and imaginary parts to create the complex number.
// complex() is an inbuilt function in GO language that takes two numbers as inputs and combines //them to produces the complex number.
var complex_num = complex(number1, number2)

// printing the resultant on the screen
fmt.Println("The resultant complex number is : ", complex_num)

// getting the real part of the complex number.
var real_part float32
real_part = real(complex_num)

// printing the real part on the screen.
fmt.Println( "The real part of the entered complex number is :" ,real_part)
}
// calling the main() function
func main() {

// defining the variables that will store the values entered by the user
var number1 float32
var number2 float32

// assigning values to the above defined variables
number1 = 53.987
number2 = -2.85

// calling the above made complex_number() function
complex_number(number1, number2)
}


### Output

The resultant complex number is : (53.987-2.85i)
The real part of the entered complex number is : 53.987


### Description of the Code

• First, we import the package fmt that allows us to print anything.

• Then we create and define the complex_number() function that will get and print the real part of the complex number.

• Start the main() function.

• Initialize and assign values to number1 and number2 variables.

• Call the complex_number() function and pass the above two float type values in it as arguments.

• Use complex() function to create a complex number from the above two values. This function takes two arguments and treats the first one as the real part of the complex number and second one as its imaginary part.

• Store the value returned by it into a separate variable. Now use real() function to get the real part of the above generated complex number and store the result in a variable. In this particular example we have used real_part variable to store the value of real part. Note that real_part is a 32 bit float type variable not a complex data type variable.

• Printing the result on the screen.

• To print the results we have used fmt.Println() function.

## Conclusion

We have successfully compiled and executed the Go language program that will get the real part of the complex number with examples.

Updated on: 15-Nov-2022

225 Views 