Go 语言计算数字幂的程序

此 Go 程序使用 math.Pow 函数来计算数字的幂。要使用此函数,您必须导入 math 模块。

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)
}
Go program to Find Power of a Number

Golang 计算数字幂的程序

在此程序中,for 循环从 1 迭代到指数值。在循环内部,我们将给定数字乘以并将结果赋给 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