Golang Program to check the power of 4 of a given number

Go ProgrammingServer Side ProgrammingProgramming

Examples

For example, n = 12 => 12 is not the power of 4.

For example, n = 64 => 64 is the power of 4.

Approach to solve this problem

Step 1 − Define a method that accepts a number n.

Step 2 − Divide log(n) by log(4), store in res.

Step 3 − If the floor of res is same as the res, then print that n is the power of 4.

Step 4 − Else, print that n is not the power of 4.

Example

 Live Demo

package main
import (
   "fmt"
   "math"
)
func CheckPowerOf4(n int){
   res := math.Log(float64(n)) / math.Log(float64(4))
   if res == math.Floor(res) {
      fmt.Printf("%d is the power of 4.\n", n)
   } else {
      fmt.Printf("%d is not the power of 4.\n", n)
   }
}
func main(){
   CheckPowerOf4(13)
   CheckPowerOf4(16)
   CheckPowerOf4(0)
}

Output

13 is not the power of 4.
16 is the power of 4.
0 is the power of 4.
raja
Published on 18-Mar-2021 05:33:12
Advertisements