how to write a programme to convert a decimal numer to hexadecimal number without using araays? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

how to write a programme to convert a decimal numer to hexadecimal number without using araays?

31st Aug 2016, 5:30 PM
ASHISH RAKESH
ASHISH RAKESH - avatar
2 Respostas
+ 3
You could use the I/O manipulator "hex": #include <iostream> int main() { int decimal =42; std::cout << "0x" << std::hex << decimal << std::endl; }
31st Aug 2016, 10:59 PM
RenƩ Schindhelm
RenƩ Schindhelm - avatar
+ 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;
31st Aug 2016, 5:45 PM
Zen
Zen - avatar