Open In App

Python program to print the hexadecimal value of the numbers from 1 to N

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to write a Python program to print the hexadecimal value of the numbers from 1 to N.

Examples:

Input: 3
Output: 1
        2
        3

Input: 11
Output: 1
        2
        3
        4
        5
        6
        7
        8
        9
        a
        b

Approach:

  • We will take the value of N as input.
  • Then, we will run the for loop from 1 to N+1 and traverse each “i” through the hex() function.
  • Print each hexadecimal value.

Note: The hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form.

Below are the implementations based on the above approach:

Python3




# Python program to print the hexadecimal value of the
# numbers from 1 to N
 
# Function to find the hexadecimal value of the numbers
# in the range 1 to N
def hex_in_range(n):
     
    # For loop traversing from 1 to N (Both Inclusive)
    for i in range(1, n+1):
         
        # Printing hexadecimal value of i
        print(hex(i)[2:])
 
# Calling the function with input 3
print("Input: 3")
hex_in_range(3)
 
# Calling the function with input 11
print("Input: 11")
hex_in_range(11)


Output

Input: 3
1
2
3
Input: 11
1
2
3
4
5
6
7
8
9
a
b

Time complexity : O(N), as it performs a loop operation for every number from 1 to N, and the time taken increases linearly with the input value N.

Space complexity : O(1), as it only uses a constant amount of memory regardless of the input value N.

Approach: String formatting approach to convert numbers to hexadecimal

  1. Take input from the user using the input function and convert it to an integer using the int function. This gives us the value of N.
  2. Loop through the numbers from 1 to N using the range function.
  3. Inside the loop, use string formatting to convert each number to its hexadecimal value. To do this, use the following expression: “{0:x}”.format(i). This expression formats the value of i as a lowercase hexadecimal string.
  4. Print the hexadecimal value using the print function.
  5. Here, the {0:x} part in the string formatting expression is a placeholder that represents the value of i. The 0 indicates the index of the value to be formatted (in this case, the first value in the parentheses), and the x specifies that the value should be formatted as a lowercase hexadecimal string.

Python3




# take input from user
n = 11
 
# loop through the numbers from 1 to N
for i in range(1, n+1):
    # use string formatting to convert the number to hexadecimal
    hex_value = "{0:x}".format(i)
    # print the hexadecimal value
    print(hex_value)


Output

1
2
3
4
5
6
7
8
9
a
b

The time complexity of the string formatting approach to convert numbers to their hexadecimal values is O(N),

The auxiliary space of this approach is O(1).

Method: Manual conversion method 

Hexadecimal is a base-16 number system, meaning that each digit can represent 16 different values (0-9 and a-f). To convert a decimal number to hexadecimal, we divide the decimal number by 16 and store the remainder as a hexadecimal digit. We then repeat this process with the quotient until the quotient becomes 0. The resulting sequence of hexadecimal digits represents the equivalent value in hexadecimal. Finally, we reverse the sequence to obtain the correct order of digits.

The steps for the manual conversion method from decimal to hexadecimal using repeated division and remainder operations are as follows:

  1. Initialize a variable to store the hexadecimal digits.
  2. Divide the decimal number by 16 and store the remainder as a hexadecimal digit.
  3. Repeat step 2 with the quotient until the quotient becomes 0.
  4. Reverse the order of the hexadecimal digits to get the final hexadecimal value.

Python3




def decimal_to_hex(n):
    if n == 0:
        return "0"
    hex_digits = "0123456789abcdef"
    hex_val = ""
    while n > 0:
        remainder = n % 16
        hex_val = hex_digits[remainder] + hex_val
        n = n // 16
    return hex_val
 
N = 11
for i in range(1, N+1):
    print(decimal_to_hex(i))


Output

1
2
3
4
5
6
7
8
9
a
b

The time complexity of the given code is O(N*log(N)), where N is the input number.
The auxiliary space complexity of the code is O(log(N)).

Approach: Using bitwise operators to convert decimal numbers to hexadecimal.

Steps:

  1. Take the input N from the user.
  2. Initialize a variable hex_digits as a string containing all the hexadecimal digits.
  3. Iterate from 1 to N.
    • For each number, initialize an empty string ‘hex_value’.
    • If the number is greater than 0, then perform the following steps:
      a. Calculate the remainder when the number is divided by 16.
      b. Add the corresponding hexadecimal digit from hex_digits to the beginning of hex_value.
      c. Right shift the number by 4 bits.
  4. Print the hexadecimal value for each number.

Below is the implementation of the above approach:

Python3




# Python program for the above approach
 
# Function to print the hexadecimal number
# from 1 to N
def printHexadecimalNumber(N):
    hex_digits = "0123456789ABCDEF"
 
    # Iterate over the range
    for num in range(1, N+1):
        hex_value = ""
 
        while num > 0:
            rem = num % 16
            hex_value = hex_digits[rem] + hex_value
 
            # Shift the number by 4 bits
            num >>= 4
 
        # Print the hex value
        print(hex_value)
 
 
# Driver Code
N = 11
 
printHexadecimalNumber(N)


Output

1
2
3
4
5
6
7
8
9
A
B

Time Complexity: O(N*log N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads