# Smallest power of 4 greater than or equal to N

Given an integer N, the task is to find the smallest power of four greater than or equal to N.

Examples:

Input: N = 12
Output: 16
24 = 16 which is the next required
greater number after 12.

Input: N = 81
Output: 81

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Find the fourth root of the given n.
2. Calculate its floor value using floor() function.
3. If n is itself a power of four then return n.
4. Else add 1 to the floor value.
5. Return the fourth power of that number.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the smallest power ` `// of 4 greater than or equal to n ` `int` `nextPowerOfFour(``int` `n) ` `{ ` `    ``int` `x = ``floor``(``sqrt``(``sqrt``(n))); ` ` `  `    ``// If n is itself is a power of 4 ` `    ``// then return n ` `    ``if` `(``pow``(x, 4) == n) ` `        ``return` `n; ` `    ``else` `{ ` `        ``x = x + 1; ` `        ``return` `pow``(x, 4); ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 122; ` `    ``cout << nextPowerOfFour(n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach  ` `import` `java.util.*; ` `import` `java.lang.Math; ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return the smallest power  ` `// of 4 greater than or equal to n  ` `static` `int` `nextPowerOfFour(``int` `n)  ` `{  ` `    ``int` `x = (``int``)Math.floor(Math.sqrt(Math.sqrt(n)));  ` ` `  `    ``// If n is itself is a power of 4  ` `    ``// then return n  ` `    ``if` `(Math.pow(x, ``4``) == n)  ` `        ``return` `n;  ` `    ``else` `{  ` `        ``x = x + ``1``;  ` `        ``return` `(``int``)Math.pow(x, ``4``);  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main (String[] args)  ` `              ``throws` `java.lang.Exception ` `{ ` `    ``int` `n = ``122``;  ` `    ``System.out.println(nextPowerOfFour(n));  ` `} ` `} ` ` `  `// This code is contributed by nidhiva `

## Python3

 `# Python implementation of above approach ` `import` `math ` ` `  `# Function to return the smallest power ` `# of 4 greater than or equal to n ` `def` `nextPowerOfFour(n): ` `    ``x ``=` `math.floor((n``*``*``(``1``/``2``))``*``*``(``1``/``2``)); ` ` `  `    ``# If n is itself is a power of 4 ` `    ``# then return n ` `    ``if` `((x``*``*``4``) ``=``=` `n): ` `        ``return` `n; ` `    ``else``: ` `        ``x ``=` `x ``+` `1``; ` `        ``return` `(x``*``*``4``); ` ` `  `# Driver code ` ` `  `n ``=` `122``; ` `print``(nextPowerOfFour(n)); ` ` `  `# This code is contributed by Rajput-Ji `

## C#

 `// C# implementation of above approach  ` `using` `System; ` `class` `GFG ` `{ ` `     `  `// Function to return the smallest power  ` `// of 4 greater than or equal to n  ` `static` `int` `nextPowerOfFour(``int` `n)  ` `{  ` `    ``int` `x = (``int``)Math.Floor(Math.Sqrt(Math.Sqrt(n)));  ` ` `  `    ``// If n is itself is a power of 4  ` `    ``// then return n  ` `    ``if` `(Math.Pow(x, 4) == n)  ` `        ``return` `n;  ` `    ``else`  `    ``{  ` `        ``x = x + 1;  ` `        ``return` `(``int``)Math.Pow(x, 4);  ` `    ``}  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main ()  ` `{ ` `    ``int` `n = 122;  ` `    ``Console.WriteLine(nextPowerOfFour(n));  ` `} ` `} ` ` `  `// This code is contributed by anuj_67.. `

Output:

```256
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : nidhiva, vt_m, Rajput-Ji

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.