Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##Reads a whole number (an integer) in base 10 and converts it to binary
- decin='xx' #to force initial read of number
- while not(decin.isnumeric()): #looks while invalid (nn numeric input)
- decin=input('enter a denary number to convert (whole number) ')
- decin=decin.strip() #strip leading and trailing spaces
- cfwd=int(decin) #copy the number and convert to integer
- if (cfwd==0):
- binaryout="0" #case of a 0 input
- else:
- binaryout="" #set initial string to blank
- while (cfwd!=0): #loop while there are bits to process
- remain=cfwd%2 #get the remainder to give next bit
- binaryout=str(remain) + binaryout #prepend that bit as a character to output string
- cfwd=cfwd//2 #float divide for next binary digit position
- print( "The value of denary ", decin, " in binary is ", binaryout)
Add Comment
Please, Sign In to add comment