- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will learn about the solution and approach to solve the given problem statement.

Given a number n, we need to check whether the given number is a power of two.

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 −

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')

No

All the variables and functions are declared in the global scope as shown below −

In this article, we learned about the approach to find whether a number is power of two or not.

- Related Questions & Answers
- 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 Find Out the Maximum Final Power of a List in Python
- Program to find whether a string is alphanumeric.
- Bash program to find A to the power B?
- 8085 program to find nth power of a number
- C++ program to find whether there is a path between two cells in matrix
- How to find power of a number in Python?
- How to Find the Power of a Number Using Recursion in Python?
- Python program to find Intersection of two lists?
- Python Program to Find the Product of two Numbers Using Recursion
- Check if bitwise AND of any subset is power of two in Python
- Python program to determine whether the given number is a Harshad Number
- C# Program to find whether the Number is Divisible by 2
- Java program to calculate the power of a number

Advertisements