Python Challenges: Check if a given positive integer is a power of two
Python Challenges - 1: Exercise-1 with Solution
Write a Python program to check if a given positive integer is a power of two.
Explanation:
Sample Solution:
Python Code:
def is_Power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
print(is_Power_of_two(4))
print(is_Power_of_two(36))
print(is_Power_of_two(16))
Sample Output:
True False True
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Python Challenges Exercises Home.
Next: Write a Python program to check if a given positive integer is a power of three.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/challenges/1/python-challenges-1-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics