0
how to write a programme to convert a decimal numer to hexadecimal number without using araays?
2 Antworten
+ 3
You could use the I/O manipulator "hex":
#include <iostream>
int main()
{
int decimal =42;
std::cout << "0x" << std::hex << decimal << std::endl;
}
+ 1
Not sure if you count string as an array, but here you go:
int n = 42;
int d;
string s = "";
while (n != 0) {
d = n % 16;
switch (d) {
case 10:
s = "a" + s;
break;
//TODO: cases 11 to 14
case 15:
s = "f" + s;
break;
default:
s = to_string(d) + s;
}
n = n / 16;
}
cout << "0x" + s;