Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent hexadecimal number. i.e convert the number with base value 10 to base value 16.
Hexadecimal numbers uses 16 values to represent a number. Numbers from 0-9 are expressed by digits 0-9 and 10-15 are represented by characters from A – F.
Input : 116 Output : 74 Input : 10 Output : A Input: 33 Output: 21
- Store the remainder when the number is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array otherwise if temp is greater than or equals to 10, insert (55 + temp) in the character array.
- Divide the number by 16 now
- Repeat the above two steps until the number is not equal to 0.
- Print the array in reverse order now.
If the given decimal number is 2545.
Step 1: Calculate remainder when 2545 is divided by 16 is 1. Therefore, temp = 1. As temp is less than 10. So, arr = 48 + 1 = 49 = ‘1’.
Step 2: Divide 2545 by 16. New number is 2545/16 = 159.
Step 3: Calculate remainder when 159 is divided by 16 is 15. Therefore, temp = 15. As temp is greater than 10. So, arr = 55 + 15 = 70 = ‘F’.
Step 4: Divide 159 by 16. New number is 159/16 = 9.
Step 5: Calculate remainder when 9 is divided by 16 is 9. Therefore, temp = 9. As temp is less than 10. So, arr = 48 + 9 = 57 = ‘9’.
Step 6: Divide 9 by 16. New number is 9/16 = 0.
Step 7: Since number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore the equivalent hexadecimal number is 9F1.
Below diagram shows an example of converting the decimal number 2545 to equivalent hexadecimal number.
Below is the implementation of above idea.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Program for Binary To Decimal Conversion
- Program for Decimal to Binary Conversion
- Decimal to binary conversion without using arithmetic operators
- Program to Convert Octal to Hexadecimal
- Program to Convert Hexadecimal Number to Binary
- Program for Fahrenheit to Celsius conversion
- Program for Celsius To Fahrenheit conversion
- Program for Fahrenheit to Kelvin conversion
- Program to convert a BCD to Hexa-Decimal Number
- Program to compute division upto n decimal places
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Convert Decimal To Hexa-Decimal including negative numbers
- Program to Convert BCD number into Decimal number
- Count Hexadecimal Number
- Check if a HexaDecimal number is Even or Odd
- Check the divisibility of Hexadecimal numbers
- Reverse bytes of a Hexadecimal Number
- Largest and Smallest N-digit Hexadecimal Numbers
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Find the count of natural Hexadecimal numbers of size N