Write a recursive function named hex that takes a nonnegativ
Write a recursive function named hex that takes a non-negative int parameter and prints the number in hexadecimal.
Solution
#include <iostream>
using namespace std;
void hex (int n){
int x = n % 16;
if (n == 0)
{
return;
}
hex(n / 16);
switch (x)
{
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<< x; break;
}
}
int main()
{
int n;
cout << \"Enter non negative number: \";
cin >> n;
hex(n);
cout<<endl;
return 0;
}
Output:
sh-4.3$ main
Enter non negative number: 115
73
