# Program to convert Decimal to Hexadecimal C++

#### Decimal Number System:

It is base 10 number system which uses the digits from 0 to 9.

It is base 16 number system which uses the digits from 0 to 9 and A, B, C, D, E, F.

### Logic to Convert Decimal to Hexadecimal C++:

1. Divide the original decimal number by 16.
2. Divide the quotient by 16.
3. Repeat the step 2 until we get quotient equal to zero.
4. Equivalent Hexadecimal number would be the remainders of each step in the reverse order.

## PROGRAM:

#include<iostream>
using namespace std;

int main()
{
long int decimalNumber,remainder,quotient;
int i=1,j,temp;

cout<<"Enter any decimal number: ";
cin>>decimalNumber;

quotient = decimalNumber;

while(quotient!=0)
{
temp = quotient % 16;

//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;

quotient = quotient / 16;
}
cout<<"nEquivalent hexadecimal number of "<<decimalNumber<<" is : ";
for(j = i -1 ;j> 0;j--)
cout<<endl;
return 0;
}

### How Code Works:

Let user enter 900.

• 900 / 16  Remainder : 4 , Quotient : 56
•  56 / 16  Remainder : 8 , Quotient : 3
•  3 / 16  Remainder : 3 , Quotient : 0

Now Hexadecimal Number is remainders in each step in reverse order i.e. 384

Hence, hexadecimal equivalent of 900 is 384.

## 2 thoughts on “Program to convert Decimal to Hexadecimal C++”

1. AG says:

can you explain this part of the code?

while(quotient!=0)
{
temp = quotient % 16;

//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;