Use any high-level programming language you wish for the following programming exercises. Do not call built-in library functions that accomplish these tasks automatically. (Examples are sprintf and sscanf from the Standard C library). 1. Write a program that receives an integer and must return a string containing the hexadecimal representation of that integer.
#include<iostream>
using namespace std;
//method to convert number to hexadecimal
string toHex(int n)
{//this is the method which converts given integer vaue to
hexadecimal string and returns
char c[] =
{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
if(n==0)
return "";
return toHex(n/16)+ ""+c[n%16];//converting to
hexadecimal string
}
int main()
{
int n;
cout<<"Enter number:";
cin>>n;
cout<<"Hexa decimal
value:"<<toHex(n)<<endl;
return 0;
}
output:
Enter number:27
Hexa decimal value:1B
Process exited normally.
Press any key to continue . . .
Get Answers For Free
Most questions answered within 1 hours.