Python program to print decimal octal hex and binary of first n numbers

Sometimes we need to display numbers in different formats: decimal, octal, hexadecimal, and binary. Python provides format specifiers to convert numbers into these representations with proper alignment.

So, if the input is like n = 10, then the output will be ?

1   1   1    1
2   2   2   10
3   3   3   11
4   4   4  100
5   5   5  101
6   6   6  110
7   7   7  111
8  10   8 1000
9  11   9 1001
10 12   A 1010

Understanding the Approach

The solution works by following these steps ?

  • Calculate the width needed based on the binary representation of n
  • For each number from 1 to n, format it in all four number systems
  • Use format specifiers: 'd' for decimal, 'o' for octal, 'X' for hexadecimal, 'b' for binary
  • Right-align each column for neat output

Complete Solution

def solve(n):
    # Calculate width based on binary representation of n
    width = len(bin(n)) - 2
    
    for i in range(1, n + 1):
        # Build format string for all four number systems
        format_str = ""
        for specifier in "doXb":
            if format_str:
                format_str += " "
            format_str += "{:>" + str(width) + specifier + "}"
        
        # Print formatted number in all four systems
        print(format_str.format(i, i, i, i))

# Test with n = 10
n = 10
solve(n)
1   1   1    1
2   2   2   10
3   3   3   11
4   4   4  100
5   5   5  101
6   6   6  110
7   7   7  111
8  10   8 1000
9  11   9 1001
10 12   A 1010

Alternative Approach Using Built-in Functions

You can also use Python's built-in functions for number conversion ?

def display_number_formats(n):
    # Calculate width for alignment
    width = len(bin(n)) - 2
    
    print(f"{'Decimal':<{width}} {'Octal':<{width}} {'Hex':<{width}} {'Binary':<{width}}")
    print("-" * (width * 4 + 3))
    
    for i in range(1, n + 1):
        decimal = str(i)
        octal = oct(i)[2:]  # Remove '0o' prefix
        hexadecimal = hex(i)[2:].upper()  # Remove '0x' prefix and convert to uppercase
        binary = bin(i)[2:]  # Remove '0b' prefix
        
        print(f"{decimal:>{width}} {octal:>{width}} {hexadecimal:>{width}} {binary:>{width}}")

# Test the function
display_number_formats(10)
Decimal Octal Hex Binary
-----------------
      1     1   1      1
      2     2   2     10
      3     3   3     11
      4     4   4    100
      5     5   5    101
      6     6   6    110
      7     7   7    111
      8    10   8   1000
      9    11   9   1001
     10    12   A   1010

How Format Specifiers Work

Specifier Number System Example (10)
d Decimal 10
o Octal 12
X Hexadecimal (uppercase) A
b Binary 1010

Conclusion

Python's format specifiers provide an elegant way to display numbers in different bases with proper alignment. The key is calculating the appropriate width and using right-alignment for neat columnar output.

Updated on: 2026-03-26T15:31:08+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements