The Program in C++ to Convert octal number to a hexadecimal number is given below:
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int decimal = 0, octal, remainder, product = 0;
string hexadecimal = "";
cout <<"Hello Codeauri Family,Enter an octal number to get converted into hexadecimal number:\n";
cin>>octal;
// convert octal to decimal
while(octal != 0) {
decimal += (octal%10) * pow(8,product);
++product;
octal /= 10;
}
// convert decimal to hexadecimal
while (decimal != 0) {
remainder = decimal % 16;
if (remainder < 10) {
hexadecimal = char(remainder + '0') + hexadecimal;
} else {
hexadecimal = char(remainder - 10 + 'A') + hexadecimal;
}
decimal /= 16;
}
cout << "Well,the number in the hexadecimal form is: " << hexadecimal;
return 0;
}
Output:
Hello Codeauri Family,Enter an octal number to get converted into hexadecimal number:
5
Well,the number in the hexadecimal form is: 5
Pro-Tips💡
This program works by first converting the input octal number to a decimal number using mathematical operations,
and then converting the decimal number to a hexadecimal number using a similar process.
The user is prompted to input an octal number, and the final hexadecimal number is outputted to the user.
The program uses remainder and a while loop to successively find the least significant digit of the decimal number and converts the digit to its corresponding hexadecimal digit.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.