fork download
  1. // def decimal_to_binary(decimal) :
  2. integer_part = int(decimal)
  3. fractional_part = decimal - integer_part
  4. binary_integer = bin(integer_part)[2:]
  5.  
  6. binary_fraction = ''
  7. while fractional_part:
  8. //fractional_part *= 2
  9. //bit = int(fractional_part)
  10. //binary_fraction += str(bit)
  11. //fractional_part -= bit
  12. //if len(binary_fraction) > 10: # Prevent infinite loop for fractions
  13. //break
  14.  
  15. //return f'{binary_integer}.{binary_fraction}' if binary_fraction else binary_integer
  16.  
  17. //def binary_to_decimal(binary) :
  18. //integer_part, _, fractional_part = binary.partition('.')
  19. //decimal_integer = int(integer_part, 2)
  20. //decimal_fraction = sum(int(bit) * 2 * *-index for index, bit in enumerate(fractional_part, start = 1))
  21.  
  22. //return decimal_integer + decimal_fraction
  23.  
  24. //def display_menu() :
  25. //print("Choose an option:")
  26. //print("1: Convert decimal to binary")
  27. //print("2: Convert binary to decimal")
  28. //return input("Enter 1 or 2: ")
  29.  
  30. //def main() :
  31. //choice = display_menu()
  32.  
  33. //if choice == '1' :
  34. //decimal = float(input("Enter a decimal number: "))
  35. //print(f'The binary equivalent is: {decimal_to_binary(decimal)}')
  36. //elif choice == '2' :
  37. //binary = input("Enter a binary number: ")
  38. //print(f'The decimal equivalent is: {binary_to_decimal(binary)}')
  39. //else:
  40. //print("Invalid choice. Please enter 1 or 2.")
  41.  
  42. //if _name_ == "_main_" :
  43. //main()
Success #stdin #stdout 0.04s 26028KB
stdin
Standard input is empty
stdout
// def decimal_to_binary(decimal) :
    integer_part = int(decimal)
    fractional_part = decimal - integer_part
    binary_integer = bin(integer_part)[2:]

    binary_fraction = ''
    while fractional_part:
//fractional_part *= 2
//bit = int(fractional_part)
//binary_fraction += str(bit)
//fractional_part -= bit
//if len(binary_fraction) > 10:  # Prevent infinite loop for fractions
//break

//return f'{binary_integer}.{binary_fraction}' if binary_fraction else binary_integer

//def binary_to_decimal(binary) :
    //integer_part, _, fractional_part = binary.partition('.')
    //decimal_integer = int(integer_part, 2)
    //decimal_fraction = sum(int(bit) * 2 * *-index for index, bit in enumerate(fractional_part, start = 1))

    //return decimal_integer + decimal_fraction

    //def display_menu() :
    //print("Choose an option:")
    //print("1: Convert decimal to binary")
    //print("2: Convert binary to decimal")
    //return input("Enter 1 or 2: ")

    //def main() :
    //choice = display_menu()

    //if choice == '1' :
        //decimal = float(input("Enter a decimal number: "))
        //print(f'The binary equivalent is: {decimal_to_binary(decimal)}')
        //elif choice == '2' :
        //binary = input("Enter a binary number: ")
        //print(f'The decimal equivalent is: {binary_to_decimal(binary)}')
    //else:
//print("Invalid choice. Please enter 1 or 2.")

//if _name_ == "_main_" :
    //main()