Convert Decimal to Hexadecimal Program in C++

Google 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 Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs