Skip to content

Commit

Permalink
Added a new algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jairam15 committed Oct 30, 2020
1 parent 43d0e60 commit 09d3274
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Python/Check whether a no is a power of two or not.py
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="")

0 comments on commit 09d3274

Please sign in to comment.