For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:
Power of a number is the product of multiplying a number by itself. Usually it is represented with a base and an exponent. The base number tells what number is being multiplied and the exponent tells how many times the base number is being multiplied. Most of the computer languages have builtin functions to find the power of numbers.
Here we are showing to find the power of a number in the Go language. Here variables num for holding the number for finding the power value, and other variable exp, for holding the exponent, and power holding the result value. The power of a number is found out by using math.Pow(num, exp). Here we must include the header file math for using this built-in function. Given below are the steps which are used in the Go program.
STEP 1: Import the package fmt, math
STEP 2: Start function main()
STEP 3: Declare the variable num, exp
STEP 4: Read the number num using fmt.Scanfln()
STEP 5: Read the exponent exp using fmt.Scanfln()
STEP 6:Find the power of a number using math.Pow(num, exp)
STEP 7: Save the result into the variable power
STEP 7: Print the result power using fmt.Println()
package main
import (
"fmt"
"math"
)
func main() {
var num, exp, power float64
fmt.Print("\nEnter the number to find the Power = ")
fmt.Scanln(& num)
fmt.Print("\nEnter the exp = ")
fmt.Scanln(& exp)
power = math.Pow(num, exp)
fmt.Print(num ,"Power", exp, " = ", power)
}
Enter the number to find the Power = 3 Enter the exp = 4 3 Power 4 = 81