
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to find whether a no is the power of two
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given a number n, we need to check whether the given number is a power of two.
Approach
Continue dividing the input number by two, i.e, = n/2 iteratively.
We will check In each iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2.
If n becomes 1 then it is a power of 2.
Let’s see the implementation below −
Example
def isPowerOfTwo(n): if (n == 0): return False while (n != 1): if (n % 2 != 0): return False n = n // 2 return True # main if(isPowerOfTwo(40)): print('Yes') else: print('No')
Output
No
All the variables and functions are declared in the global scope as shown below −
Conclusion
In this article, we learned about the approach to find whether a number is power of two or not.
- Related Articles
- Python Program to find whether a no is power of two
- C++ Program to find whether a number is the power of two?
- Program to check a number is power of two or not in Python
- JavaScript program to check whether a given number is power of 2
- Program to find out the value of a power of 2 in Python
- Program to find Reordered Power of 2 in Python
- Program to Find Out the Maximum Final Power of a List in Python
- Program to find out the vertical area between two points where no point lies and is the widest in Python
- C++ program to find whether there is a path between two cells in matrix
- Program to find whether a string is alphanumeric.
- Bash program to find A to the power B?
- Find whether a given integer is a power of 3 or not in C++
- Find whether a given number is a power of 4 or not in C++
- Program to find best team with no conflicts in Python
- 8085 program to find nth power of a number

Advertisements