To convert a number from the decimal system to the hexadecimal system in C++ programming language, the following code example can be used.
#include <iostream>
using namespace std;
int main() {
int num, remainder, decimal, product = 1;
char c;
string hex = "";
cout << "Please enter a number: ";
cin >> num;
decimal = num;
while (decimal != 0) {
remainder = decimal % 16;
if (remainder >= 10) {
c = remainder + 55;
} else {
c = remainder + 48;
}
hex += c;
decimal = decimal / 16;
product *= 10;
}
reverse(hex.begin(), hex.end());
cout << "Hexadecimal value of number: " << num << " is: " << hex << endl;
}
Please enter a number: 1234
Hexadecimal value of number: 1234 is: 4D2