Write a C++ program to convert a Decimal number to its corresponding Hexadecimal number.
Program Code:
#include<iostream>
using namespace std;
int main()
{
long int deci, rem, q;
char hexa[100];
int i=1, j, temp;
cout<<"Enter any decimal number : ";
cin>>deci;
cout<<endl;
q = deci;
while(q != 0)
{
temp = q % 16;
if(temp < 10)
{
temp = temp + 48;
}
else
{
temp = temp + 55;
}
hexa[i++] = temp;
q = q / 16;
}
cout<<"Equivalent hexadecimal value of "<<deci<<" is : ";
for(j=i-1; j>0; j--)
{
cout<<hexa[j];
}
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter any decimal number : 45
Equivalent hexadecimal value of 45 is : 2D
Thanks
Mukesh Rajput
Post A Comment:
0 comments: