Python: Print four values decimal, octal, hexadecimal, binary in a single line of a given integer
Print number in decimal, octal, hex, binary.
Write a Python program to print four integer values - decimal, octal, hexadecimal (capitalized), binary - in a single line.
Sample Solution:
Python Code:
# Get user input for an integer
i = int(input("Input an integer: "))
# Convert the integer to octal and remove the '0o' prefix
o = str(oct(i))[2:]
# Convert the integer to hexadecimal and remove the '0x' prefix, then capitalize the result
h = str(hex(i))[2:].upper()
# Convert the integer to binary and remove the '0b' prefix
b = str(bin(i))[2:]
# Convert the integer to a string for decimal representation
d = str(i)
# Print the formatted output with headers
print("Decimal Octal Hexadecimal (capitalized), Binary")
print(d,' ',o,' ',h,' ',b)
Sample Output:
Input an integer: 25 Decimal Octal Hexadecimal (capitalized), Binary 25 31 19 11001
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to convert an integer into its decimal, octal, hexadecimal (uppercase), and binary representations and print them on one line.
- Write a Python program to use format specifiers to display an integer in multiple number bases simultaneously.
- Write a Python program to implement a function that returns a formatted string showing the decimal, octal, hex, and binary formats of a number.
- Write a Python program to use f-string formatting to output an integer in all four number systems with proper labels.
Python Code Editor:
Previous: Write a Python program to wrap a given string into a paragraph of given width.
Next: Write a Python program to swap cases of a given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics