Python program to print the hexadecimal value of the numbers from 1 to N
Last Updated :
04 Apr, 2023
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
def hex_in_range(n):
for i in range ( 1 , n + 1 ):
print ( hex (i)[ 2 :])
print ( "Input: 3" )
hex_in_range( 3 )
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
- 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.
- Loop through the numbers from 1 to N using the range function.
- 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.
- Print the hexadecimal value using the print function.
- 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
n = 11
for i in range ( 1 , n + 1 ):
hex_value = "{0:x}" . format (i)
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:
- Initialize a variable to store the hexadecimal digits.
- Divide the decimal number by 16 and store the remainder as a hexadecimal digit.
- Repeat step 2 with the quotient until the quotient becomes 0.
- 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:
- Take the input N from the user.
- Initialize a variable hex_digits as a string containing all the hexadecimal digits.
- 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.
- Print the hexadecimal value for each number.
Below is the implementation of the above approach:
Python3
def printHexadecimalNumber(N):
hex_digits = "0123456789ABCDEF"
for num in range ( 1 , N + 1 ):
hex_value = ""
while num > 0 :
rem = num % 16
hex_value = hex_digits[rem] + hex_value
num >> = 4
print (hex_value)
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
Share your thoughts in the comments
Please Login to comment...