Convert Decimal to Hexadecimal Program in C++

Advertisements

Prev Tutorial Next Tutorial

C++ program to Convert Decimal Number to Hexadecimal

In this code we takes a decimal number and converts it into its hexadecimal equivalent. Decimal number have base 10 and Hexadecimal number have base 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.

Steps to Convert Decimal to Hexadecimal

  • A decimal number is entered
  • Using a while loop, the number is divided by 16 and the hexadecimal equivalent of the remainders are stored
  • The result is printed in reverse order
  • Exit

Example

Input Decimal Number : 143
Euivalent Hexadecimal Number : 8F

Input Decimal Number : 63
Euivalent Hexadecimal Number : 3F

Input Decimal Number : 123
Euivalent Hexadecimal Number : 7B

c++ program to convert Decimal to Hexadecimal

#include<iostream.h>
#include<conio.h>

void main()
{
int num,rem[20],hex=0,i=0,j;
clrscr();
cout<<"Enter any Decimal Number: ";
cin>>num;
while(num>0)
  {
    rem[i] = num % 16;
    num = num / 16;
    i++;
  }
cout<<"Equivalent Hexadecimal Number: ";
for(j = i - 1 ; j > = 0 ; j--)
  {
    switch(rem[j])
    {
      case 10:
	  cout<<"A";
	  break;
      case 11:
	  cout<<"B";
	  break;
      case 12:
	  cout<<"C";
	  break;
      case 13:
	  cout<<"D";
	  break;
      case 14:
	  cout<<"E";
	  break;
      case 15:
	  cout<<"F";
	  break;
      default :
	 cout<<rem[j];
    }
  }
getch();
}

Output

Enter any Decimal Number : 83
Equivalent Hexadecimal number : 53

Output

Enter any Decimal Number : 125
Equivalent Hexadecimal number : 7D

Output

Enter any Decimal Number : 205
Equivalent Hexadecimal number : CD
c++ program to convert decimal number into hexadecimal
Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements