Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Program to calculate the power of a number in Go (Golang)

Posted on January 29, 2023January 29, 2023 by admin

Table of Contents

  • Overview
  • Program

Overview

The objective is to calculate the power of a given integer. There will be two inputs

  • The number itself – The number can be positive as well as negative. It can be float as well
  • The power – The power also can be positive as well as negative

Examples

Input: Num:2, Power:4
Output: 16
Input: Num:2, Power:-4
Output: 0.0625

Program

Here is the program for the same.

package main
import "fmt"
func pow(x float64, n int) float64 {
if x == 0 {
return 0
}
if n == 0 {
return 1
}
if n == 1 {
return x
}
if n == -1 {
return 1 / x
}
val := pow(x, n/2)
m := x
if n < 0 {
m = 1 / x
}
if n%2 == 1 || n%2 == -1 {
return val * val * m
} else {
return val * val
}
}
func main() {
output := pow(2, 4)
fmt.Println(output)
output = pow(2, -4)
fmt.Println(output)
}

Output

16
0.0625

Note: Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang - Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you -All Design Patterns Golang

  • go
  • golang
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP