[Golang] Integer Exponentiation


Integer exponentiation in Go programming language.

Run Code on Go Playground

package main

import (
      "fmt"
)

// return a^n
func Power(a, n uint) uint {
      var i, result uint
      result = 1
      for i = 0; i < n; i++ {
              result *= a
      }
      return result
}

func main() {
      fmt.Println(Power(2, 0))
      fmt.Println(Power(2, 1))
      fmt.Println(Power(2, 2))
      fmt.Println(Power(2, 3))
}

Tested on: Go Playground


References:

[1]