Advertisement
CodeCrusader

Decimal Number To Binary Code

Jun 10th, 2023
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. def decimal_to_binary(decimal):
  2.     try:
  3.         decimal = int(decimal)
  4.     except ValueError:
  5.         print("Invalid input. Please enter a valid integer.")
  6.         return
  7.  
  8.     if decimal < 0:
  9.         print("Decimal number cannot be negative.")
  10.         return
  11.  
  12.     binary = bin(decimal)[2:]  # Remove the '0b' prefix from the binary representation
  13.     return binary
  14.  
  15. decimal_number = input("Enter a decimal number: ")
  16.  
  17. binary_representation = decimal_to_binary(decimal_number)
  18. if binary_representation:
  19.     print(f"Binary representation: {binary_representation}")
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement