# Golang : Check if integer is power of four example

Tags : golang math-pow power-of

**Problem:**

You need to test if a given integer is a power of 4. For example, if given number = 16, return true and if number = 9 return false.

**Solution:**

Test to see if the number can be divided by 4 until it is 1.

```
package main
import (
"fmt"
"math"
)
func isPowerOfFour(num int) bool {
for num >= 4 {
if num%4 != 0 {
return false
}
num = num / 4
}
return num == 1
}
func main() {
fmt.Println("Is 9 power of 4? : ", isPowerOfFour(9))
fmt.Println("Is 64 power of 4? : ", isPowerOfFour(64))
fmt.Println("Is 8 power of 4? : ", isPowerOfFour(8))
// if use math.Pow() function, remember to convert float64
// to int
testNumPowerOfFour := int(math.Pow(2, 4))
fmt.Println("Is 16 power of 4? : ", isPowerOfFour(testNumPowerOfFour))
testNumPowerOfFour = int(math.Pow(2, 3))
fmt.Println("Is 8 power of 4? : ", isPowerOfFour(testNumPowerOfFour))
}
```

Output:

Is 9 power of 4? : false

Is 64 power of 4? : true

Is 8 power of 4? : false

Is 16 power of 4? : true

Is 8 power of 4? : false

**References:**

https://www.socketloop.com/tutorials/golang-math-pow-the-power-of-x-y-example

#### See also : Golang : Math pow(the power of x^y) example

Tags : golang math-pow power-of

##### By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+3.9k Golang : automatically figure out array length(size) with three dots

+3.9k Golang : Detect number of faces or vehicles in a photo

+2.2k Golang : Issue HTTP commands to server and port example

+5.2k Golang : Compress and decompress file with compress/flate example

+6.9k Golang : How to read float value from standard input ?

+2.3k How to check with curl if my website or the asset is gzipped ?

702 Linux : How to set root password in Linux Mint

+7.8k SSL : The certificate is not trusted because no issuer chain was provided

+3.7k Golang : Get UDP client IP address and differentiate clients by port number

+3k Swift : Get substring with rangeOfString() function example

+2.2k Golang : Find change in a combination of coins example

+1.1k Golang : Calculate pivot points for a cross