Given an integer n, find whether it is a power of 4 or not.
Example :
Input : 16 Output : 16 is a power of 4 Input : 20 Output : 20 is not a power of 4
We strongly recommend that you click here and practice it, before moving on to the solution.
1. A simple method is to take a log of the given number on base 4, and if we get an integer then the number is the power of 4.
2. Another solution is to keep dividing the number by 4, i.e, do n = n/4 iteratively. In any iteration, if n%4 becomes non-zero and n is not 1 then n is not a power of 4, otherwise, n is a power of 4.
C++
// C++ program to find whether a given // number is a power of 4 or not#include<iostream>using namespace std;#define bool intclass GFG{ /* Function to check if x is power of 4*/public : bool isPowerOfFour(int n){ if(n == 0) return 0; while(n != 1) { if(n % 4 != 0) return 0; n = n / 4; } return 1;}};/*Driver code*/int main(){ GFG g; int test_no = 64; if(g.isPowerOfFour(test_no)) cout << test_no << " is a power of 4"; else cout << test_no << "is not a power of 4"; getchar();}// This code is contributed by SoM15242 |
C
#include<stdio.h>#define bool int/* Function to check if x is power of 4*/bool isPowerOfFour(int n){ if(n == 0) return 0; while(n != 1) { if(n % 4 != 0) return 0; n = n / 4; } return 1;} /*Driver program to test above function*/int main(){ int test_no = 64; if(isPowerOfFour(test_no)) printf("%d is a power of 4", test_no); else printf("%d is not a power of 4", test_no); getchar();} |
Java
// Java code to check if given// number is power of 4 or notclass GFG { // Function to check if // x is power of 4 static int isPowerOfFour(int n) { if(n == 0) return 0; while(n != 1) { if(n % 4 != 0) return 0; n = n / 4; } return 1; } // Driver program public static void main(String[] args) { int test_no = 64; if(isPowerOfFour(test_no) == 1) System.out.println(test_no + " is a power of 4"); else System.out.println(test_no + "is not a power of 4"); }}// This code is contributed// by prerna saini |
Python3
# Python3 program to check if given# number is power of 4 or not # Function to check if x is power of 4def isPowerOfFour(n): if (n == 0): return False while (n != 1): if (n % 4 != 0): return False n = n // 4 return True# Driver codetest_no = 64if(isPowerOfFour(64)): print(test_no, 'is a power of 4')else: print(test_no, 'is not a power of 4')# This code is contributed by Danish Raza |
C#
// C# code to check if given// number is power of 4 or notusing System;class GFG { // Function to check if // x is power of 4 static int isPowerOfFour(int n) { if (n == 0) return 0; while (n != 1) { if (n % 4 != 0) return 0; n = n / 4; } return 1; } // Driver code public static void Main() { int test_no = 64; if (isPowerOfFour(test_no) == 1) Console.Write(test_no + " is a power of 4"); else Console.Write(test_no + " is not a power of 4"); }}// This code is contributed by Sam007 |
PHP
<?php// PHP code to check if given// number is power of 4 or not// Function to check if// x is power of 4function isPowerOfFour($n) { if($n == 0) return 0; while($n != 1) { if($n % 4 != 0) return 0; $n = $n / 4; } return 1;} // Driver Code$test_no = 64;if(isPowerOfFour($test_no)) echo $test_no," is a power of 4";else echo $test_no," is not a power of 4";// This code is contributed by Rajesh?> |
Output :
64 is a power of 4
3. A number n is a power of 4 if the following conditions are met.
a) There is only one bit set in the binary representation of n (or n is a power of 2)
b) The count of zero bits before the (only) set bit is even.
For example 16 (10000) is the power of 4 because there is only one bit set and count of 0s before the set bit is 4 which is even.
Thanks to Geek4u for suggesting the approach and providing the code.
C++
// C++ program to check // if given number is // power of 4 or not #include<bits/stdc++.h>using namespace std;bool isPowerOfFour(unsigned int n){ int count = 0; /*Check if there is only one bit set in n*/ if ( n && !(n&(n-1)) ) { /* count 0 bits before set bit */ while(n > 1) { n >>= 1; count += 1; } /*If count is even then return true else false*/ return (count%2 == 0)? 1 :0; } /* If there are more than 1 bit set then n is not a power of 4*/ return 0;} /*Driver code*/int main(){ int test_no = 64; if(isPowerOfFour(test_no)) cout << test_no << " is a power of 4" ; else cout << test_no << " is not a power of 4";}// This code is contributed by Shivi_Aggarwal |
C
#include<stdio.h>#define bool intbool isPowerOfFour(unsigned int n){ int count = 0; /*Check if there is only one bit set in n*/ if ( n && !(n&(n-1)) ) { /* count 0 bits before set bit */ while(n > 1) { n >>= 1; count += 1; } /*If count is even then return true else false*/ return (count%2 == 0)? 1 :0; } /* If there are more than 1 bit set then n is not a power of 4*/ return 0;} /*Driver program to test above function*/int main(){ int test_no = 64; if(isPowerOfFour(test_no)) printf("%d is a power of 4", test_no); else printf("%d is not a power of 4", test_no); getchar();} |
Java
// Java program to check // if given number is // power of 4 or not import java.io.*;class GFG { static int isPowerOfFour(int n) { int count = 0; /*Check if there is only one bit set in n*/ int x = n & (n - 1); if ( n > 0 && x == 0) { /* count 0 bits before set bit */ while(n > 1) { n >>= 1; count += 1; } /*If count is even then return true else false*/ return (count % 2 == 0) ? 1 : 0; } /* If there are more than 1 bit set then n is not a power of 4*/ return 0; } // Driver Code public static void main(String[] args) { int test_no = 64; if(isPowerOfFour(test_no)>0) System.out.println(test_no + " is a power of 4"); else System.out.println(test_no + " is not a power of 4"); }}// This code is contributed by mits |
Python3
# Python3 program to check if given# number is power of 4 or not # Function to check if x is power of 4def isPowerOfFour(n): count = 0 # Check if there is only one # bit set in n if (n and (not(n & (n - 1)))): # count 0 bits before set bit while(n > 1): n >>= 1 count += 1 # If count is even then return # true else false if(count % 2 == 0): return True else: return False# Driver codetest_no = 64if(isPowerOfFour(64)): print(test_no, 'is a power of 4')else: print(test_no, 'is not a power of 4')# This code is contribued by Danish Raza |
C#
// C# program to check if given// number is power of 4 or not using System;class GFG { static int isPowerOfFour(int n) { int count = 0; /*Check if there is only one bit set in n*/ int x = n & (n-1); if ( n > 0 && x == 0) { /* count 0 bits before set bit */ while(n > 1) { n >>= 1; count += 1; } /*If count is even then return true else false*/ return (count % 2 == 0) ? 1 : 0; } /* If there are more than 1 bit set then n is not a power of 4*/ return 0; } /*Driver program to test above function*/ static void Main() { int test_no = 64; if(isPowerOfFour(test_no)>0) Console.WriteLine("{0} is a power of 4", test_no); else Console.WriteLine("{0} is not a power of 4", test_no); }}// This Code is Contributed by mits |
PHP
<?phpfunction isPowerOfFour($n){ $count = 0;/*Check if there is only one bit set in n*/if ( $n && !($n&($n-1)) ){ /* count 0 bits before set bit */ while($n > 1) { $n >>= 1; $count += 1; } /*If count is even then return true else false*/ return ($count%2 == 0)? 1 :0;}/* If there are more than 1 bit set then n is not a power of 4*/return 0;} /*Driver program to test above function*/ $test_no = 64; if(isPowerOfFour($test_no)) echo $test_no, " is a power of 4";else echo $test_no, " not is a power of 4"; #This Code is Contributed by Ajit?> |
Output:
64 is a power of 4
4. A number n is a power of 4 if the following conditions are met.
a) There is only one bit set in the binary representation of n (or n is a power of 2)
b) The bits don’t AND(&) any part of the pattern 0xAAAAAAAA
For example: 16 (10000) is power of 4 because there is only one bit set and 0x10 & 0xAAAAAAAA is zero.
Thanks to Sarthak Sahu for suggesting the approach.
C++
// C++ program to check // if given number is // power of 4 or not #include<bits/stdc++.h>using namespace std;bool isPowerOfFour(unsigned int n){ return n !=0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);} /*Driver code*/int main(){ int test_no = 64; if(isPowerOfFour(test_no)) cout << test_no << " is a power of 4" ; else cout << test_no << " is not a power of 4";} |
C
// C program to check // if given number is // power of 4 or not #include<stdio.h>#define bool intbool isPowerOfFour(unsigned int n){ return n != 0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);} /*Driver program to test above function*/int main() { int test_no = 64; if(isPowerOfFour(test_no)) printf("%d is a power of 4", test_no); else printf("%d is not a power of 4", test_no); getchar();} |
Java
// Java program to check // if given number is // power of 4 or not import java.io.*;class GFG { static boolean isPowerOfFour(int n) { return n != 0 && ((n&(n-1)) == 0) && (n & 0xAAAAAAAA) == 0; } // Driver Code public static void main(String[] args) { int test_no = 64; if(isPowerOfFour(test_no)) System.out.println(test_no + " is a power of 4"); else System.out.println(test_no + " is not a power of 4"); }} |
Python3
# Python3 program to check # if given number is # power of 4 or not def isPowerOfFour(n): return (n != 0 and ((n & (n - 1)) == 0) and not(n & 0xAAAAAAAA));# Driver codetest_no = 64;if(isPowerOfFour(test_no)): print(test_no ,"is a power of 4");else: print(test_no , "is not a power of 4");# This code contributed by Rajput-Ji |
C#
// C# program to check // if given number is // power of 4 or not using System;class GFG{ static bool isPowerOfFour(int n) { return n != 0 && ((n&(n-1)) == 0) && (n & 0xAAAAAAAA) == 0; } // Driver Code static void Main() { int test_no = 64; if(isPowerOfFour(test_no)) Console.WriteLine("{0} is a power of 4", test_no); else Console.WriteLine("{0} is not a power of 4", test_no); }}// This code is contributed by mohit kumar 29 |
Output:
64 is a power of 4
Why 0xAAAAAAAA ? This is because the bit representation is of powers of 2 that are not of 4. Like 2, 8, 32 so on.
5. A number will be a power of 4 if floor(log4(num))=ceil(log4(num) because log4 of a number that is a power of 4 will always be an integer.
Below is the implementation of the above approach.
C++
// C++ program to check // if given number is // power of 4 or not #include<bits/stdc++.h> using namespace std; float logn(int n, int r){ return log(n) / log(r);}bool isPowerOfFour(int n) { //0 is not considered as a power //of 4 if(n == 0) return false; return floor(logn(n,4))==ceil(logn(n,4)); } /*Driver code*/int main() { int test_no = 64; if(isPowerOfFour(test_no)) cout << test_no << " is a power of 4" ; else cout << test_no << " is not a power of 4"; return 0;} |
Java
// Java program to check // if given number is // power of 4 or not import java.util.*;class GFG{static double logn(int n, int r) { return Math.log(n) / Math.log(r);}static boolean isPowerOfFour(int n) { // 0 is not considered // as a power of 4 if (n == 0) return false; return Math.floor(logn(n, 4)) == Math.ceil(logn(n, 4));}// Driver code public static void main(String[] args) { int test_no = 64; if (isPowerOfFour(test_no)) System.out.print(test_no + " is a power of 4"); else System.out.print(test_no + " is not a power of 4");}}// This code is contributed by Amit Katiyar |
Python3
# Python3 program to check# if given number is# power of 4 or notimport mathdef logn(n, r): return math.log(n) / math.log(r)def isPowerOfFour(n): # 0 is not considered # as a power of 4 if (n == 0): return False return (math.floor(logn(n, 4)) == math.ceil(logn(n, 4)))# Driver codeif __name__ == '__main__': test_no = 64 if (isPowerOfFour(test_no)): print(test_no, " is a power of 4") else: print(test_no, " is not a power of 4")# This code is contributed by Amit Katiyar |
C#
// C# program to check // if given number is // power of 4 or not using System;class GFG{static double logn(int n, int r) { return Math.Log(n) / Math.Log(r);}static bool isPowerOfFour(int n) { // 0 is not considered // as a power of 4 if (n == 0) return false; return Math.Floor(logn(n, 4)) == Math.Ceiling(logn(n, 4));}// Driver code public static void Main(String[] args) { int test_no = 64; if (isPowerOfFour(test_no)) Console.Write(test_no + " is a power of 4"); else Console.Write(test_no + " is not a power of 4");}}// This code is contributed by 29AjayKumar |
Output:
64 is a power of 4
Please write comments if you find any of the above codes/algorithms incorrect, or find other ways to solve the same problem.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find whether a given integer is a power of 3 or not
- Check whether a given Number is Power-Isolated or not
- Check if given number is a power of d where d is a power of 2
- Find power of power under mod of a prime
- Find the sum of power of bit count raised to the power B
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Program to find whether a no is power of two
- Compute power of power k times % m
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Check whether the given number is Euclid Number or not
- Check whether given number N is a Moran Number or not
- Check whether a given number N is a Nude Number or not
- Check whether a given number is an ugly number or not
- Queries to find whether a number has exactly four distinct factors or not
- Check whether a given number is Polydivisible or Not
- Check whether the given number is Wagstaff prime or not
- Check whether a number has consecutive 0's in the given base or not
- Determine whether the given integer N is a Peculiar Number or not
- Check whether the binary equivalent of a number ends with given string or not
- Check Whether a number is Duck Number or not

