Golang Program to print the absolute value of complex numbers

Go ProgrammingProgrammingServer Side Programming

In this tutorial, we will learn how to print the absolute value of Complex numbers in Go programming language.

The absolute value of a number is the non-negative value of the number without regard to its sign. For example, the absolute value of 15 is 15, and the absolute value of −10 is also 10. The absolute value of a number refers to be its distance from zero along real number line.

Syntax

func Abs(a complex128) float64

We can find the absolute value of the specified complex number with the help of Abs() function provided by the math/cmplx package. We need to import a math/cmplx package in your program to access the Abs() function.

Go Programming language provides an inbuilt support for basic constants and mathematical functions for complex numbers with the complex package.

Printing Absolute value in the Main function

Algorithm

Step 1 − Import the package fmt, math/cmplx

Step 2 − Start function main()

Step 3 − Declare the float variable ‘z’

Step 4 − Find the absolute value of a given number using cmplx.Abs () formula

Step 5 − Use a value of complex number with cmplx.Abs() formula

Step 6 − Print the result using fmt.Println()

Example

The following is an example in which we find out the absolute value of a complex number within the function

package main // fmt package provides the function to print anything import "fmt" // math/cmplx package is used for Absolute value formula of a complex number import "math/cmplx" func main() { // defining the variable var z float64 fmt.Println("Golang Program to print the absolute value of Complex numbers.") // defining the Abs formula of a complex number z = cmplx.Abs(-44+7i) // complex initializer syntax a + ib // printing the result fmt.Println("The Absolute value of the Complex numbers is",z) }

Output

Golang Program to print the absolute value of Complex numbers
The Absolute value of the Complex numbers is 44.55333881989093

Description of code

  • First we Import the package fmt, and math/cmplx package which is used for the Absolute value formula of a complex number

  • Then we start the function main()

  • var z float64 − In this line of code we have to declare the variable that will store the absolute value of a number given by the user.

  • Then we are calling cmplx.Abs () function using a complex value and store its result in the z variable.

  • And last printing the result on the screen after finding the absolute value of a complex number using fmt.Println.

Printing Absolute value using two different functions

Algorithm

Step 1 − Import the package fmt, math/cmplx

Step 2 − Start AbsCmplx function()

Step 3 − Declare the variable ‘z’ and defining the absolute value formula of a complex number

Step 4 − Start function main()

Step 5 − Defining the variable f

Step 6 − Calling the AbsCmplx function()

Step 7 − Print the result using fmt.Println()

Example

The following is an example in which we find out the absolute value of a complex number in two separate functions −

package main // fmt package provides the function to print anything import "fmt" // math/cmplx package is used for Absolute value formula of a complex number import "math/cmplx" // Creating AbsCmplx function() func AbsCmplx(a complex128) float64 { // defining the float variable var z float64 // defining the Abs formula of a complex number z = cmplx.Abs(a) return z } func main() { var f float64 fmt.Println("Golang Program to print the absolute value of Complex numbers") // calling AbsCmplx function f = AbsCmplx(43+8i) // printing the result fmt.Println("The Absolute value of the Complex numbers is",f) }

Output

Golang Program to print the absolute value of Complex numbers
The Absolute value of the Complex numbers is 43.73785545725808

Description of code

  • First we Import the package fmt, math/cmplx, where math/cmplx package is used for the absolute value formula of a complex number

  • Then we are create func AbsCmplx() function to find the absolute value of a complex number.

  • We declare a variable ‘z’ and define the cmplx.Abs formula

  • Then we start the function main().

  • In the next step, we are declaring a float variable ‘f’ that will store the absolute value of a complex number given by the user.

  • Then we are calling AbsCmplx function() which we have created outside the main function using a complex value and store its result in the floating variable f.

  • And last printing the result on the screen after finding the absolute value of a complex number using fmt.Println.

Conclusion

We have successfully compiled and executed the Golang program code to print the absolute value of Complex numbers in the above two examples

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

Advertisements