HomePythonPython Program to Find Whether a Number is a Power of Two

Python Program to Find Whether a Number is a Power of Two

You can determine whether a number is a power of two by checking if it has only one bit set in its binary representation. If only one bit is set, then it’s a power of two. Here’s a Python program to do this with an introduction:

Program Statement

Write a Python program that checks whether a given number is a power of two. The program should prompt the user to enter a number, and then it should determine if the entered number is a power of two or not. If it is a power of two, the program should display a message indicating that the number is a power of two. Otherwise, it should display a message indicating that the number is not a power of two.

Otherwise, it should display a message indicating that the number is not a power of two. The program should continue to prompt the user for numbers until they choose to exit.

The program should have the following features:

  • Define a function called is_power_of_two that takes an integer n as input and returns True if n is a power of two, and False otherwise.
  • Inside the is_power_of_two function, use a loop to repeatedly divide n by 2 until it becomes 1, checking for divisibility by 2 in each iteration.
  • Prompt the user to enter a number.
  • Call the is_power_of_two function with the entered number and display an appropriate message indicating whether it is a power of two or not.
  • Provide an option for the user to exit the program.

That’s the program statement. You can use it as a guideline to implement the Python program to determine whether a number is a power of two.

Python Program to Find Whether a Number is a Power of Two

def is_power_of_two(n):
    # Check if n is positive and non-zero
    if n <= 0:
        return False

    # Keep dividing n by 2 until it becomes 1
    while n > 1:
        # Check if n is divisible by 2
        if n % 2 != 0:
            return False
        n = n // 2

    return True

# Test the function
number = int(input("Enter a number: "))
if is_power_of_two(number):
    print(number, "is a power of two.")
else:
    print(number, "is not a power of two.")

How it works

Here’s a step-by-step explanation of how the Python program works:

  1. The program defines a function called is_power_of_two that takes an integer n as input and returns True if n is a power of two, and False otherwise.
  2. Inside the is_power_of_two function, it first checks if the number n is less than or equal to 0. If it is, it immediately returns False, as powers of two are always positive.
  3. The function enters a loop that continues until n becomes 1. This loop is responsible for checking the divisibility of n by 2.
  4. Inside the loop, it checks if n is divisible by 2 by using the modulo operator (n % 2 != 0). If n is not divisible by 2 (i.e., the remainder is not 0), it means n is not a power of two, so the function returns False.
  5. If n is divisible by 2, it updates the value of n by dividing it by 2 using the floor division operator (n = n // 2). This process continues until n becomes 1.
  6. Once the loop ends, it means n has become 1, and the function returns True to indicate that the number is a power of two.
  7. The main part of the program prompts the user to enter a number.
  8. The program calls the is_power_of_two function with the entered number as an argument.
  9. It checks the return value of the function. If it is True, the program displays a message indicating that the number is a power of two. If it is False, it displays a message indicating that the number is not a power of two.
  10. After displaying the result, the program gives an option for the user to exit or continue entering numbers.
  11. If the user chooses to continue, the program repeats the steps of prompting, calling the function, and displaying the result.

That’s how the program works! It repeatedly checks whether the entered numbers are powers of two and provides the corresponding output until the user decides to exit.

Input/Output

Python Program to Find Whether a Number is a Power of Two

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...