Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to check the power of 4 of a given number
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
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.
Advertisements