-4

How can i loop through a decimal number and print out a binary figure for that decimal number. This is the code i tried using python

dec_Num = 1200
for i in dec_Num;
print i
  • 2
    `bin()` converts decimal numbers to binary – 0x263A Jan 18 '22 at 23:45
  • 2
    Your question is not clear. What is the expected result? – Barmar Jan 18 '22 at 23:45
  • 2
    Maybe this answers your question: https://stackoverflow.com/questions/69702530/using-a-loop-to-convert-decimal-to-binary – Mark Jan 18 '22 at 23:46
  • Welcome to Stack Overflow! Please take the [tour]. Please [edit] your question to clarify what you're trying to do exactly. Is the loop important, or do you just want to print the number as binary? For more tips, see [ask]. Also, what does this have to do with `return`? BTW, are you aware that [Python 2 is EOL](https://www.python.org/doc/sunset-python-2/)? – wjandrea Jan 18 '22 at 23:52

1 Answers1

0

Because the goal is to print we can generate a string

def DecimalToBinary(num):
    strin=""

     
   
    while num >= 1:
       strin+=str(num%2)

        num=num // 2

     return strin[::-1]
DanielGzgzz
  • 118
  • 1
  • 6