Decimal to Hexadecimal Conversion

Decimal to Hexadecimal Conversion” is a one of the classic programming problem exercise for beginners. Here, we are given a Decimal Number, entered by user and our task is to write a program to convert the given Decimal Number to its equivalent Hexadecimal Number.

Hexadecimal Numbers uses 16 values to represent the number. Numbers from 0-9 are expressed by digits 0-9 and 10-15 are represented by characters from A – F

Examples (Decimal to Hexadecimal Conversion):

Decimal = 15
Hexadecimal = A
Decimal = 78
Hexadecimal = 4E

The steps required for decimal to hexadecimal conversion are as follows:

  1. Initialize an empty string which will store hexadecimal number.
  2. Store the remainder when the decimal number is divisible by 16.
  3. If the remainder is less than 10, then convert the remainder number into character by (remainder + 48) and insert it to hexadecimal string at the front.
  4. Else if the remainder is greater than 9, then convert the remainder number into character by (remainder + 55) and insert it to hexadecimal string at the front.
  5. Divide the decimal number by 16.
  6. Repeat the steps from 2 to 5 until decimal number is greater than 0.
  7. Print the Hexadecimal number.

C++ Program for Decimal to Hexadecimal Conversion is as follows:

/* C++ Program for Decimal to Hexadecimal Conversion */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan the Decimal Number */  
    int Decimal;  
    cout<<"Enter the Decimal Number: ";  
    cin>>Decimal;  
      
    /* Converting Decimal to Hexadecimal */  
    string Hexadecimal;  
    int temp = Decimal;  
    while(temp!=0)  
    {  
        int remainder = temp % 16;  
        if(remainder >=0 && remainder <=9)  
        {  
            char ch = remainder + 48;  
            Hexadecimal = ch + Hexadecimal;  
        }  
        else  
        {  
            char ch = remainder + 55;  
            Hexadecimal = ch + Hexadecimal;  
        }  
        temp = temp / 16;  
    }  
      
    /*Printing the result */  
    cout<<"The Hexadecimal of "<<Decimal<<" is "<<Hexadecimal;  
}  
OUTPUT:
Enter the Decimal Number: 78
The Hexadecimal of 78 is 4E

Related Posts:

  1. Program to convert decimal number to binary number.
  2. Program to convert decimal number to octal number.
  3. Program to convert hexadecimal number to binary number.
  4. Program to convert hexadecimal number to octal number.
  5. Program to convert hexadecimal number to decimal number.
  6. Program to convert binary number to octal number.
  7. Program to convert binary number to decimal number.
  8. Program to convert binary number to hexadecimal number.
  9. Program to convert octal number to binary number.
  10. Program to convert octal number to decimal number.
  11. Program to convert octal number to hexadecimal number.
  12. Program to print Fibonacci Series up to Nth Term.
  13. Program to find and print nth Fibonacci number.
  14. Program to find Power of the Number.
  15. Program to find Quotient and Remainder.
  16. Program to find largest amongst three numbers.
  17. Program to find factorial of a number.
  18. Program to find GCD of two numbers.
  19. Program to find LCM of two numbers.
  20. Program to check whether entered number is odd or even.

You may also like...