Files
thealgorithms--c-plus-plus/others/decimal_to_hexadecimal.cpp
T
wehub-resource-sync 29cfe479ab
Awesome CI Workflow / Code Formatter (push) Waiting to run
Awesome CI Workflow / Compile checks (macOS-latest) (push) Blocked by required conditions
Awesome CI Workflow / Compile checks (ubuntu-latest) (push) Blocked by required conditions
Awesome CI Workflow / Compile checks (windows-latest) (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 12:37:28 +08:00

35 lines
969 B
C++

/**
* @file
* @brief Convert decimal number to hexadecimal representation
*/
#include <iostream>
/**
* Main program
*/
int main(void) {
int valueToConvert = 0; // Holds user input
int hexArray[8]; // Contains hex values backwards
int i = 0; // counter
char HexValues[] = "0123456789ABCDEF";
std::cout << "Enter a Decimal Value"
<< std::endl; // Displays request to stdout
std::cin >>
valueToConvert; // Stores value into valueToConvert via user input
while (valueToConvert > 15) { // Dec to Hex Algorithm
hexArray[i++] = valueToConvert % 16; // Gets remainder
valueToConvert /= 16;
// valueToConvert >>= 4; // This will divide by 2^4=16 and is faster
}
hexArray[i] = valueToConvert; // Gets last value
std::cout << "Hex Value: ";
while (i >= 0) std::cout << HexValues[hexArray[i--]];
std::cout << std::endl;
return 0;
}