-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'''Write a program to find whether a given number is a power of 2 or not. | ||
Output Format: | ||
Print 'YES' or 'NO' accordingly | ||
Example: | ||
Input: | ||
64 | ||
Output: | ||
YES | ||
Input: | ||
48 | ||
Output: | ||
NO | ||
Explanation: | ||
In the first example, 64 is a power of 2 so the answer is YES. | ||
The second number is not a power of 2 hence the answer is NO.''' | ||
|
||
#The Code | ||
|
||
import math #math module for mathematical functions | ||
n=int(input("Enter the number")) | ||
def log2(n): #find log2 of the given number | ||
l=math.log10(n)/math.log10(2) | ||
return(l) | ||
def isPower(n): #checking if a number is power of two | ||
return (math.ceil(log2(n)) == math.floor(log2(n))) #comparisson of floor and ceil values | ||
if(isPower(n)): #printing output | ||
print("YES", end="") | ||
else: | ||
print("NO", end="") |