In: Computer Science
C++ contains some built-in functions (such as itoa and std::hex) which make this assignment trivial. You may NOT use these in your programs. You code must perform the conversion through your own algorithm.
The input values are:
5
9
24
2
39
83
60
8
11
10
6
18
31
27
#include<iostream>
using namespace std;
//method to convert to binary
int toBinary(int n)
{
if(n==0)
return 0;
return toBinary(n/2)*10 + n%2;
}
//method to convert number to hexadecimal
string toHex(int n)
{
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];
}
int main()
{
int n;
cout<<"Enter how many number u want to
enter:";
cin>>n;
cout<<"Enter numbers:";
int m,i=1;
while(i<=n)
{
cout<<i<<":";
cin>>m;
cout<<"Binary
:"<<toBinary(m)<<endl;
cout<<"Hexadecimal
:"<<toHex(m)<<endl;
i++;
}
return 0;
}
output:
Enter how many number u want to enter:14
Enter numbers:1:5
Binary :101
Hexadecimal :5
2:9
Binary :1001
Hexadecimal :9
3:24
Binary :11000
Hexadecimal :18
4:2
Binary :10
Hexadecimal :2
5:39
Binary :100111
Hexadecimal :27
6:83
Binary :1010011
Hexadecimal :53
7:60
Binary :111100
Hexadecimal :3C
8:8
Binary :1000
Hexadecimal :8
9:11
Binary :1011
Hexadecimal :B
10:10
Binary :1010
Hexadecimal :A
11:6
Binary :110
Hexadecimal :6
12:18
Binary :10010
Hexadecimal :12
13:31
Binary :11111
Hexadecimal :1F
14:27
Binary :11011
Hexadecimal :1B
Process exited normally.
Press any key to continue . . .