- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

# Golang Program to print the absolute value of complex numbers

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

- Related Questions & Answers
- Calculate the absolute value of complex numbers in Numpy
- Golang program to calculate the absolute and scale of a vertex.
- Golang Program to Print the Sum of all the Positive Numbers and Negative Numbers in a List
- Golang Program to Print Odd Numbers Within a Given Range
- How to calculate the absolute values in a pandas series with complex numbers?
- C# Program to get the absolute value of the time
- Golang Program to Read a Number (n) and Print the Natural Numbers Summation Pattern
- Golang Program to Read Two Numbers and Print their Quotient and Remainder
- Python program for Complex Numbers
- Golang Program to Print the Multiplication Table of a Given Number
- Write a Golang program to print the Fibonacci series
- Golang Program to Print an Identity Matrix
- Golang Program to Print the Numbers in a Range (1, upper) without Using any Loops
- Java Program to Add Two Complex numbers
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- Golang Program to Print an Inverted Star Pattern