Check for Power of Two in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>

int isPowerOfTwo(unsigned int n) {
    if (n == 0) return 0;
    if (n == 1) return 1;
    if (n % 2 != 0) return 0;
    return isPowerOfTwo(n / 2);
}

int main() {
    unsigned int num = 16;
    if (isPowerOfTwo(num))
        printf("%u is a power of two\n", num);
    else
        printf("%u is not a power of two\n", num);
    return 0;
}

C Output

16 is a power of two


C++ Program

#include <iostream>
using namespace std;

bool isPowerOfTwo(unsigned int n) {
    if (n == 0) return false;
    if (n == 1) return true;
    if (n % 2 != 0) return false;
    return isPowerOfTwo(n / 2);
}

int main() {
    unsigned int num = 18;
    if (isPowerOfTwo(num))
        cout << num << " is a power of two\n";
    else
        cout << num << " is not a power of two\n";
    return 0;
}

C++ Output

18 is not a power of two


JAVA Program

public class Main {
    static boolean isPowerOfTwo(int n) {
        if (n == 0) return false;
        if (n == 1) return true;
        if (n % 2 != 0) return false;
        return isPowerOfTwo(n / 2);
    }

    public static void main(String[] args) {
        int num = 8;
        if (isPowerOfTwo(num))
            System.out.println(num + " is a power of two");
        else
            System.out.println(num + " is not a power of two");
    }
}

JAVA Output

8 is a power of two


Python Program

def is_power_of_two(n):
    if n == 0:
        return False
    if n == 1:
        return True
    if n % 2 != 0:
        return False
    return is_power_of_two(n // 2)

num = 20
if is_power_of_two(num):
    print(f"{num} is a power of two")
else:
    print(f"{num} is not a power of two")

Python Output

20 is not a power of two


Explanation
Example
Let's use the number 16. If we continue to divide it by 2, we have 8, then 4, then 2, then 1. At which time we can stop and verify it's a power of two. Attempt the same thing with 18 and you'll notice it doesn't hold because you'll end up with a decimal or an odd number somewhere along the line.

Real-Life Analogy
Think of keeping folding a perfectly square piece of paper in half every time. If you can just keep doing that and get all the way down to one square without cutting or tearing, it's like a power-of-two shape — flawless halves all the way down. If the paper won't fold cleanly into halves at some point, it's like a number that is not a power of two.

Why It Matters
Powers of two permeate computer science — memory capacities (1KB, 2KB, 4KB.) and data structures such as heaps, and divide-and-conquer algorithms. Knowing how to determine if a number is a power of two efficiently is not only a mathematical trick, but a useful skill for system optimization.

Learning Insights
This exercise emphases recursion as a means of dividing a large problem into smaller same-sized problems. It also establishes the connection between binary and powers of two: a power of two in binary always contains exactly one set bit (e.g., 8 is 1000). Knowing this, you can even solve it in O(1) time with n && !(n & (n-1)).

Interview and Real-World Usage
This problem appears in coding interviews in two forms: by recursion and by bitwise operations. The candidate should be able to solve it in both of these ways. Power-of-two checking is frequent in real life in memory management, image processing (power-of-two textures), and networking (packet sizes).

SEO-Friendly Closing
Verifying whether a number is a power of two through recursion is a basic functionality programming drill that further reinforces knowledge of recursion, divisibility, and binary numbers. This topic applies extensively in algorithms, data structures, and system programming where binary optimization is important. In C, C++, Java, or Python, knowing this technique ensures developers can code more efficiently and cleanly for interviews or real-world projects.