Back to Practice Dashboard
Python BasicsEasy
Decimal to Hexadecimal Conversion
Learn how to solve the 'Decimal to Hexadecimal Conversion' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function decimal_to_hex(n) that takes a non-negative integer n and returns a string representing its hexadecimal (base-16) equivalent using uppercase letters (A-F). Do not include leading zeros (except for input 0, which should return '0').
To convert, repeatedly divide by 16 and collect remainders (using A=10, B=11, ..., F=15) in reverse order.
Constraints
- •0 <= n <= 10^6
Examples
Example 1
Input
decimal_to_hex(255)
Output
'FF'
Explanation
255 ÷ 16 = 15 remainder 15. 15 = F. So result is FF.
Example 2
Input
decimal_to_hex(26)
Output
'1A'
Explanation
26 ÷ 16 = 1 remainder 10. 10 = A. Result: 1A.
Example 3
Input
decimal_to_hex(100)
Output
'64'
Explanation
100 ÷ 16 = 6 remainder 4. Result: 64.
Need a Hint?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.