Created
September 30, 2020 03:28
-
-
Save Abhayparashar31/892eae1d6cde1d43b690a5540697e6ad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Stack(): | |
def __init__(self): | |
self.items = [] ## Empty list intilize | |
def push(self,item): | |
self.items.append(item) ## simple appending to list | |
def pop(self): | |
return self.items.pop() ## Removing top element of the stack | |
def is_empty(self): | |
return self.items==[] ### Checking whether the stack is empty or not | |
def peek(self): | |
if not self.is_empty(): | |
return self.items[-1] ## returnign top element using [-1]=last element | |
def show_stack(self): | |
return self.items ## Printing all the items | |
number = int(input("Enter an Integer")) | |
def div_by_2(number): | |
s = Stack() ## object for stack | |
while number > 0: ## While number is greate than 0 | |
rem = number % 2 ## divide the number by 2 | |
s.push(rem) ## PUSH the rem on to stack | |
number = number // 2 ## divide the number by //2 so that we can get integer as output | |
bin_num = '' | |
while not s.is_empty(): ## Until stack is not empty | |
bin_num += str(s.pop()) ## Pop each element from the stack and concatenate it | |
return bin_num ## Return binary representation | |
print(div_by_2(number)) ## Print the binary number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment