# Golang Program to Find the Absolute value of a given 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 will learn how to find the absolute value of a given number 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 10 is 10, and the absolute value of −15 is also 15. The absolute value of a number refers to be its distance from zero along real number line.

### Syntax

x = math.Abs( x )
func Abs(x float64) float64


Absolute Abs() is a function in Go language. The Abs() function returns the absolute / positive value of any given number (positive or negative).

the input value is already a positive number then that will remain unchanged. If the function input value is a complex number (x + yi) then Abs() will convert its output into a floating point value. The Abs function returns a single value of type float64. This value represents the absolute value of the given argument.

Some Special Cases −

Abs(±Inf) = +Inf (Here inf means infinity)
Abs(NaN) = NaN (NaN means not a number)


## Finding the absolute value of an integer variable

### Algorithm

Step 1 − Import the package fmt, math

Step 2 − Start function main()

Step 3 − Declare the variable ‘x’

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

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

### Example

The following is an example in which we find out the absolute value of an integer variable

package main

// fmt package provides the function to print anything
import "fmt"

// math package is used for Absolute value formula
import "math"
func main() {

// defining the variable
var x float64
fmt.Println("Program to find the absolute value of a given number.")

// defining the Abs formula
x = math.Abs( -44)

// printing the result
fmt.Println("The Absolute value of a given number is",x)
}


### Output

Program to find the absolute value of a given number
The Absolute value of a given number is 44


### Description of code

• First we Import the package fmt, math

• Then we start the function main()

• var x 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 math.Abs() function and store its value in the x variable.

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

## Finding the absolute value of a complex number

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

### Algorithm

Step 1 − Import the package fmt, math/cmplx

Step 2 − Start function main()

Step 3 − Declare the variable ‘z’

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

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

### Example

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

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("Program to find the absolute value of a complex number.")

// defining the Abs formula of a complex number
z = cmplx.Abs(-56+8i) // complex initializer syntax a + bi

// printing the result
fmt.Println("The Absolute value of a given number is",z)
}


### Output

Program to find the absolute value of a complex number.
The Absolute value of a given number is 56.568542494923804


### 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 and store its value in the z variable.

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

## Finding absolute value using 2 different functions

### Algorithm

Step 1 − Import the package fmt, math

Step 2 − Start Abs function()

Step 3 − Declare the variable ‘a’ and defining the absolute value formula

Step 4 − Start function main()

Step 5 − Defining the variable f

Step 6 − Calling the Abs function() and Print the result using fmt.Println()

### Example

In this example, we will define and perform the operation in 2 different functions −

package main
// fmt package provides the function to print anything

import "fmt"
// math package is used for the Absolute value formula
import "math"

// Creating Abs function()
func Abs(a float64) float64 {

// defining the Abs formula
a = math.Abs(a)
return a
}
func main() {

// define the float64 variable
var f float64
fmt.Println("Program to find the absolute value of a given number.")

// calling Abs function
f = Abs(-19.63)

// printing the result
fmt.Println("The Absolute value of a given number is",f)
}


### Output

Program to find the absolute value of a given number
The Absolute value of a given number is 19.63


### Description of code

• First we Import the package fmt, math, where math package is used for the absolute value formula.

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

• We declare a variable ‘a’ and define the maths Absolute value 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 number given by the user.

• Then we are calling Abs function() which we have created outside the main function and store its result in the floating variable f.

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

### Conclusion

We have successfully compiled and executed the Golang program code to find the absolute value of a given number in three examples

Updated on 11-Oct-2022 08:07:18