How to Calculate power or exponents of a Number in Golang Example

In this post, You will learn Three programs to calculate a power of a number.

  • First, the program is to calculate the power of a number
  • Second program to find the exponent of a number using the Math pow function
  • The third program is to find the power of a number using a recursive function

The power of m with exponent n also called m power n, is a product of the number n with m times.
For example, 4 power 3 is called 4 to a power of 3
Exponent is 3, Base is 5
Power of 3 bases 5 is equal to 5*5*5=125.

The power of a number always returns a positive number.

The below two programs take two input numbers(base and exponent) from the user using the Scanln function, and store these numbers in two variables - exponent and base.

To understand this example, You should have the following features in the Go language.

How to calculate the power of a number using Golang for loop

In this program, Iterated the exponent number using the condition exponent != 0
Inside for loop, the exponent is multiplied with base times and stored the result in the variable output
Finally, the Output is printed to the console.

Here is a program to check the exponent of a number

package main

import (
    "fmt"
)

func main() {
    var exponent, base int
    fmt.Print("Enter Base:")
    fmt.Scanln( & base)
    fmt.Print("Enter exponent:")
    fmt.Scanln( & exponent)

    output: = 1
    for exponent != 0 {
        output *= base
        exponent -= 1
    }
    fmt.Printf("The Output of power calculation is %d", output)
}

Output is

Enter Base:4
Enter exponent:3
The output of power calculation is 64

The above program works only with integer numbers of the base.
Alternatively, You can use the math pow() function which works with any numbers.

How to find the exponent of a number using golang Math pow function

Package math provides mathematical functions. pow() function is used to calculate the power of a given exponent and base.

Here is the syntax of the pow function

func Pow(x, y float64) float64

Here is a math pow function example to find the power of a number

package main
import (
    "fmt"
    "math"
)

func main() {
    var exponent, base float64
    fmt.Print("Enter Base:")
    fmt.Scanln( & base)
    fmt.Print("Enter exponent:")
    fmt.Scanln( & exponent)

    output: = math.Pow(base, exponent)

    fmt.Printf("Output of power calculation is %f", output)

}

Output:

Enter Base:5
Enter exponent:3
The output of power calculation is 125.000000

How to calculate the power of a number using golang recursive function

A recursive function is a function that calls inside a recursive function.

Initially RecurisveFunction is called from the main function for the first time.

Inside a function, It calls until the exponent is zero and the result is returned from a function.

Here is a Recursion function for the power of a number.

package main

import (
    "fmt"
)

func RecursivePower(base int, exponent int) int {
    if exponent != 0 {
        return (base * RecursivePower(base, exponent - 1))
    } else {
        return 1
    }
}

func main() {
    var exponent, base int
    fmt.Print("Enter Base:")
    fmt.Scanln( & base)
    fmt.Print("Enter exponent:")
    fmt.Scanln( & exponent)

    output: = RecursivePower(base, exponent)

    fmt.Printf("Output of power calculation is %d", output)

}

Output:

Enter Base:5
Enter exponent:3
The output of power calculation is 125.000000