Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program that will read in an integer number 2. It will recursively sh

ID: 3622626 • Letter: 1

Question

1. Write a program that will read in an integer number
2. It will recursively show the conversion between the decimal number and the Hexadecimal number (base 16).
3. Example program output:

Please enter the decimal number : 430
The decimal number 43 is 2B in hexadecimal
4. Hexadecimal symbols for decimal 10-15 are A-F respectively

Heres my code:
#include
using namespace std;
string to16(int);
int main()
{
string ans;
int num;
cout<<"Enter integer you want to convert: ";
cin>>num;
cout<<<" in base 16 is "<<
system("pause");
return 0;
}
string to16(int num)
{char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char ch;
if(num==0)
return " ";
ch=hex[num%16];
return to16(num/16)+ch;
}

My prof says, that its not correct because I have potentially infinite recursion for that.

It should look similar to this example:
#include
using namespace std;
void displayBin(int pval);

main ()
{ int val;
cout<<" What is the value? ";
cin>>val;
cout<<" The value is "<<
cout<<"The value in binary is ";
displayBin(val);
cin.get();
cin.get();
return 0;
}
void displayBin(int pval)
{int idiv,irmdr;
irmdr=pval%2;
idiv = pval/2;
if (idiv>0) displayBin(idiv); // base case clear ending point
cout<
return;
}

Lifesaver to correct answer

Explanation / Answer

// compiles and runs fine.

#include<iostream>
#include<string>
string to16(int num)
{char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char ch;
if(num==0)
return " ";

ch=hex[num%16];
cout << ch;                  // this displays each conversion.
return to16(num/16)+ch;
}
int main()
{
string ans;
int num;

cout<<"Enter integer you want to convert: ";
cin>>num;

cout<<" in base 16 is "<<to16(num)<<endl;   // this gives hexa number.
return 0;
}