Program for decimal to hexadecimal conversion - GeeksforGeeks

06 Apr, 2022
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.
Examples:  
Input : 116
Output: 74

Input : 10
Output: A

Input : 33
Output: 21
Algorithm:
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 nowRepeat the above two steps until the number is not equal to 0.Print the array in reverse order now.
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.
Example
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[0] = 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[1] = 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[2] = 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 an equivalent hexadecimal number.  
Below is the implementation of above idea. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to convert a decimal// number to hexadecimal number #include <iostream>using namespace std; // function to convert decimal to hexadecimalvoid decToHexa(int n){    // char array to store hexadecimal number    char hexaDeciNum[100];     // counter for hexadecimal number array    int i = 0;    while (n != 0) {        // temporary variable to store remainder        int temp = 0;         // storing remainder in temp variable.        temp = n % 16;         // check if temp < 10        if (temp < 10) {            hexaDeciNum[i] = temp + 48;            i++;        }        else {            hexaDeciNum[i] = temp + 55;            i++;        }         n = n / 16;    }     // printing hexadecimal number array in reverse order    for (int j = i - 1; j >= 0; j--)        cout << hexaDeciNum[j];} // Driver program to test above functionint main(){    int n = 2545;     decToHexa(n);     return 0;}
// Java program to convert a decimal// number to hexadecimal numberimport java.io.*; class GFG {    // function to convert decimal to hexadecimal    static void decToHexa(int n)    {        // char array to store hexadecimal number        char[] hexaDeciNum = new char[100];         // counter for hexadecimal number array        int i = 0;        while (n != 0) {            // temporary variable to store remainder            int temp = 0;             // storing remainder in temp variable.            temp = n % 16;             // check if temp < 10            if (temp < 10) {                hexaDeciNum[i] = (char)(temp + 48);                i++;            }            else {                hexaDeciNum[i] = (char)(temp + 55);                i++;            }             n = n / 16;        }         // printing hexadecimal number array in reverse        // order        for (int j = i - 1; j >= 0; j--)            System.out.print(hexaDeciNum[j]);    }     // driver program    public static void main(String[] args)    {        int n = 2545;        decToHexa(n);    }} // Contributed by Pramod Kumar
# Python3 program to# convert a decimal# number to hexadecimal# number # function to convert# decimal to hexadecimal  def decToHexa(n):     # char array to store    # hexadecimal number    hexaDeciNum = ['0'] * 100     # counter for hexadecimal    # number array    i = 0    while(n != 0):         # temporary variable        # to store remainder        temp = 0         # storing remainder        # in temp variable.        temp = n % 16         # check if temp < 10        if(temp < 10):            hexaDeciNum[i] = chr(temp + 48)            i = i + 1        else:            hexaDeciNum[i] = chr(temp + 55)            i = i + 1        n = int(n / 16)     # printing hexadecimal number    # array in reverse order    j = i - 1    while(j >= 0):        print((hexaDeciNum[j]), end="")        j = j - 1  # Driver Coden = 2545decToHexa(n) # This code is contributed# by mits.
// C# program to convert a decimal// number to hexadecimal numberusing System; class GFG {    // function to convert decimal    // to hexadecimal    static void decToHexa(int n)    {        // char array to store        // hexadecimal number        char[] hexaDeciNum = new char[100];         // counter for hexadecimal number array        int i = 0;        while (n != 0) {            // temporary variable to            // store remainder            int temp = 0;             // storing remainder in temp            // variable.            temp = n % 16;             // check if temp < 10            if (temp < 10) {                hexaDeciNum[i] = (char)(temp + 48);                i++;            }            else {                hexaDeciNum[i] = (char)(temp + 55);                i++;            }             n = n / 16;        }         // printing hexadecimal number        // array in reverse order        for (int j = i - 1; j >= 0; j--)            Console.Write(hexaDeciNum[j]);    }     // Driver Code    public static void Main(String[] args)    {        int n = 2545;        decToHexa(n);    }} // This code is contributed by Nitin Mittal.
<?php// PHP program to convert// a decimal number to// hexadecimal number // function to convert// decimal to hexadecimalfunction decToHexa($n){    // char array to store    // hexadecimal number    $hexaDeciNum;         // counter for hexadecimal    // number array    $i = 0;    while($n != 0)    {        // temporary variable        // to store remainder        $temp = 0;                 // storing remainder        // in temp variable.        $temp = $n % 16;                 // check if temp < 10        if($temp < 10)        {            $hexaDeciNum[$i] = chr($temp + 48);            $i++;        }        else        {            $hexaDeciNum[$i] = chr($temp + 55);            $i++;        }                 $n = (int)($n / 16);    }         // printing hexadecimal number    // array in reverse order    for($j = $i - 1; $j >= 0; $j--)        echo $hexaDeciNum[$j];} // Driver Code$n = 2545;decToHexa($n); // This code is contributed// by mits.?>
<script> // Javascript program to convert a decimal// number to hexadecimal number // function to convert decimal to hexadecimalfunction decToHexa(n){    // char array to store hexadecimal number    var hexaDeciNum = Array.from({length: 100},                      (_, i) => 0);     // counter for hexadecimal number array    var i = 0;    while (n != 0) {        // temporary variable to store remainder        var temp = 0;         // storing remainder in temp variable.        temp = n % 16;         // check if temp < 10        if (temp < 10) {            hexaDeciNum[i] =            String.fromCharCode(temp + 48);            i++;        }        else {            hexaDeciNum[i] =            String.fromCharCode(temp + 55);            i++;        }         n = parseInt(n / 16);    }     // printing hexadecimal number array in reverse    // order    for (j = i - 1; j >= 0; j--)        document.write(hexaDeciNum[j]);} // driver programvar n = 2545;decToHexa(n); // This code contributed by shikhasingrajput </script>
9F1
Using Predefined function
C++
Java
Python3
C#
Javascript
// C++ program to convert decimal number to hexadecimal#include <iostream>using namespace std; // function to convert decimal number to hexadecimalvoid decToHexa(int n) { cout << hex << n << endl; } // driver codeint main(){    int n = 2545;    decToHexa(n);    return 0;} // This code is contributed by phasing17.
// Java program to convert a decimal// number to hexadecimal numberimport java.io.*; class GFG {    public static void decToHexa(int n)    {        System.out.println(Integer.toHexString(n));    }    public static void main(String[] args)    {         int n = 2545;        decToHexa(n);    }}
# Python program to convert a decimal# number to hexadecimal number # function to convert decimal number# to equivalent hexadecimal numberdef decToHexa(n):  return hex(n).replace("0x","") # Driver Coden = 2545print(decToHexa(n)) # This code is contributed by shahidedu7.
// C# program to convert a decimal// number to hexadecimal numberusing System; class GFG {    public static void decToHexa(int n)    {        Console.Write(Convert.ToString(n));    }    public static void Main(String[] args)    {         int n = 2545;        decToHexa(n);    }} // This code is contributed by shivanisinghss2110
<script>// JS program to convert a decimal// number to hexadecimal number // Function to convert decimal to hexadecimal base integerfunction decToHexa(n){    document.write(n.toString(16));} // Driver Codevar n = 2545;decToHexa(n); // This code is contributed by phasing17</script>
9f1
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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. 
nitin mittal
Mithun Kumar
le0
shikhasingrajput
shahidedu7
shivanisinghss2110
phasing17
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
Operators in C / C++
The Knight's tour problem | Backtracking-1
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects