How to convert a decimal number into a binary number using STACK in python

nsm2015

Here is my code.

class Stack ():

   def __init__(self):
        self.items=[]

    def push (self,item):
        self.items.append(item)

    def pop (self):
        return self.items.pop()

    def size(self):
        return len(self.items)

s1= Stack()




decNum= int(input("Enter the decimal num : "))

newNum=decNum

while newNum==1:

    newNum= decNum%2
    decNum = decNum//2
    s1.push(newNum)
    while s1.size() is not Null:
        a=s1.pop()
        print(a)
Anand S Kumar

First of all, the above code you have pasted has lots of indentation issues, next time you should try to fix those indentation issues and running your program again, before posting it here.

Secondly, your logic is also wrong, you should not be checking -

while newNum == 1:

at start itself, you are setting newNum = decNum , unless you enter 1 , your program would not enter that while loop. The condition you need is -

while decNum != 0:

Secondly, the second while loop, should not be inside the first while loop, it should be outside, so that you print out the stack once all the computation is complete. Also , you don't even need the first newNum = decNum , before the loop starts.

I fixed all your indentation issues, and logical issues, the code is -

import sys
class Stack ():
    def __init__(self):
        self.items=[]
    def push (self,item):
        self.items.append(item)
    def pop (self):
        return self.items.pop()
    def size(self):
        return len(self.items)
s1= Stack()
decNum= int(raw_input("Enter the decimal num : "))
while decNum!=0:
    newNum= decNum%2
    decNum = decNum//2
    s1.push(newNum)
while s1.size() != 0:
    a=s1.pop()
    sys.stdout.write(str(a))
sys.stdout.flush()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a decimal number to a binary number using recursive functions?

convert 32 bit, binary number to decimal in python

Simple function to convert decimal number to binary in Python

How to convert a decimal number to binary in Swift?

How to convert a Decimal to Number(with decimal numbers) in Python

How to convert a sympy decimal number to a Python decimal?

convert a binary number (consists of the sign, mantissa and exponent ) to decimal number in python

How to convert a binary fraction number into decimal fraction number?

How to convert a binary fraction number into decimal fraction number in R?

Convert negative binary number to decimal

How to convert a decimal to binary in C programming using stack

How do I convert a very long binary number to decimal?

How to convert binary number into vector using MatLab?

How to convert a negative exponential number into a decimal number in Python?

How to Convert Fixed Point Number to Decimal Number in Python?

VHDL : how to transform a Binary number into a decimal number

C program to convert a decimal number to binary string

Convert decimal to binary with billions number in C

Convert decimal number provided in String to the binary

How to convert a decimal number into fraction?

Convert decimal number to binary number and change one index

convert a number or string to binary number in python?

How can I convert a decimal number into binary, saving binary digits in array?

using AWK, how do I convert a decimal number to hexadecimal

MySQL - How to convert number to decimal using stored precision/scale?

Converting Decimal Number To Binary In Python 3

How to convert binary number to array?

How To Convert From Decimal to Octal Without Changing The Digits Of The Number? (python)

How to convert a full number to 2 decimal places in python and mysql?