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)).

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

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.

Similar Reads

Approach: String formatting approach to convert numbers to hexadecimal

...

Method: Manual conversion method

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....

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

...