C++ Program to convert decimal to hexadecimal
Program
#include<iostream>
using namespace std;
void decimal_hexa(int num)
{
int rem;
int base = 16;
long int h = 0;
if (num != 0)
{
rem = num % base;
decimal_hexa(num/base);
if(rem >= 10)
{
cout << rem+55;
}
else
{
cout << rem;
}
}
}
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Hexadecimal equivalent is : ";
decimal_hexa(num);
return 0;
}
Output
$ g++ convert-decimal-to-hexa.cpp
$ ./a.out
Enter a number: 256
Hexadecimal equivalent is : 100