This Go program uses the math pow function to find the power of a number. To use this function, you have to import the math module.
package main import ( "fmt" "math" ) func main() { var pnum, expo float64 fmt.Print("\nEnter the Number to find the Power = ") fmt.Scanln(&pnum) fmt.Print("Enter the Exponent Value = ") fmt.Scanln(&expo) power := math.Pow(pnum, expo) fmt.Println(pnum, " Power ", expo, " = ", power) }
Golang program to Find Power of a Number
In this golang program, the for loop iterate numbers from 1 to exponent value. Within that, we are multiplying the given number and assigning it to power.
package main import "fmt" func main() { var i, pnum, expo, power int power = 1 fmt.Print("\nEnter the Number to find the Power = ") fmt.Scanln(&pnum) fmt.Print("Enter the Exponent Value = ") fmt.Scanln(&expo) for i = 1; i <= expo; i++ { power = power * pnum } fmt.Println(pnum, " Power ", expo, " = ", power) }
SureshMac:GoExamples suresh$ go run power2.go
Enter the Number to find the Power = 10
Enter the Exponent Value = 2
10 Power 2 = 100
SureshMac:GoExamples suresh$ go run power2.go
Enter the Number to find the Power = 3
Enter the Exponent Value = 4
3 Power 4 = 81