codescracker


c++

C++ Program to Convert Decimal to Hexadecimal



« Previous Program Next Program »


Decimal to Hexadecimal Conversion in C++

To convert a number from decimal format into equivalent hexadecimal format in C++ programming, you have to ask to the user to enter the desired decimal number to convert it into hexadecimal number to display the equivalent value in hexadecimal format as shown here in the following program.

C++ Programming Code to Convert Decimal to Hexadecimal

Following C++ program ask to the user to enter any number in decimal to convert it into hexadecimal, then display the result on the screen :

/* C++ Program - Decimal to Hexadecimal Conversion */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	long int decnum, rem, quot;
	char hexdecnum[100];
	int i=1, j, temp;
	cout<<"Enter any decimal number : ";
	cin>>decnum;
	quot=decnum;
	while(quot!=0)
	{
		temp=quot%16;
		// to convert integer into character
		if(temp<10)
		{
			temp=temp+48;
		}
		else
		{
			temp=temp+55;
		}
		hexdecnum[i++]=temp;
		quot=quot/16;
	}
	cout<<"Equivalent hexadecimal value of "<<decnum<<" is : \n";
	for(j=i-1; j>0; j--)
	{
		cout<<hexdecnum[j];
	}
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program convert decimal to hexadecimal

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test