Python BasicsEasy

Decimal to Hexadecimal Conversion

Detailed guide and Python implementation for the 'Decimal to Hexadecimal Conversion' problem.

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?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.