Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

C++ Program For Decimal To Hexadecimal Conversion

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:

  1. 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.
  2. Divide the number by 16 now
  3. Repeat the above two steps until the number is not equal to 0.
  4. 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++




// C++ program to convert a decimal
// number to hexadecimal number
#include <iostream>
using namespace std;
  
// Function to convert decimal 
// to hexadecimal
void 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 code
int main()
{
    int n = 2545;
    decToHexa(n);
    return 0;
}

Output:

9F1

Time complexity: O(log16n)
Auxiliary space: O(1)

Using Predefined function

C++




// C++ program to convert decimal 
// number to hexadecimal
#include <iostream>
using namespace std;
  
// Function to convert decimal 
// number to hexadecimal
void decToHexa(int n) 
   cout << hex << n << endl; 
}
  
// Driver code
int main()
{
    int n = 2545;
    decToHexa(n);
    return 0;
}
// This code is contributed by phasing17.

Output:

9f1


My Personal Notes arrow_drop_up
Last Updated : 17 Jan, 2023
Like Article
Save Article
Similar Reads