C++ Exercises: Convert a hexadecimal number to octal number
C++ For Loop: Exercise-81 with Solution
Write a C++ program to convert a hexadecimal number to an octal number.
Visual Presentation:
Sample Solution:-
C++ Code :
#include<iostream> // Include necessary libraries
#include<stdlib.h>
#include<math.h>
using namespace std; // Use the standard namespace
// Function to convert hexadecimal to decimal
unsigned long Hex_To_Dec(char hex[]) {
char *hexstring;
int length = 0;
const int hexbase = 16; // Define base 16 for hexadecimal
unsigned long dnum = 0;
int i;
// Calculate the length of the hexadecimal string
for (hexstring = hex; *hexstring != '\0'; hexstring++) {
length++;
}
hexstring = hex; // Reset the pointer to the start of the hexadecimal string
// Convert each character of the hexadecimal string to decimal
for (i = 0; *hexstring != '\0' && i < length; i++, hexstring++) {
if (*hexstring >= 48 && *hexstring <= 57) {
// Convert character to decimal if it's a number (0-9)
dnum += (((int)(*hexstring)) - 48) * pow(hexbase, length - i - 1);
}
else if ((*hexstring >= 65 && *hexstring <= 70)) {
// Convert character to decimal if it's an uppercase letter (A-F)
dnum += (((int)(*hexstring)) - 55) * pow(hexbase, length - i - 1);
}
else if (*hexstring >= 97 && *hexstring <= 102) {
// Convert character to decimal if it's a lowercase letter (a-f)
dnum += (((int)(*hexstring)) - 87) * pow(hexbase, length - i - 1);
}
else {
// Display an error message for an invalid input character
cout<<" The given hexadecimal number is invalid. \n";
}
}
return dnum; // Return the resulting decimal number
}
// Main function
int main() {
unsigned long dnum;
char hex[9]; // Array to store the input hexadecimal number
int dec_num, rem = 1, m, n;
int oct_num[100], quot;
dec_num = 0;
cout << "\n\n Convert any hexadecimal number to octal number:\n"; // Display purpose of the program
cout << "----------------------------------------------------\n"; // Display separator line
cout << " Input any 32-bit Hexadecimal Number: ";
cin >> hex; // Read the hexadecimal input from the user
dnum = Hex_To_Dec(hex); // Call the function to convert hexadecimal to decimal
quot = dnum;
cout << " The equivalent octal number is: ";
// Conversion from decimal to octal
while (quot != 0) {
oct_num[m++] = quot % 8;
quot = quot / 8;
}
// Display the resulting octal number
for (n = m - 1; n >= 0; n--) {
cout << oct_num[n];
}
cout << "\n"; // Print a newline
}
Sample Output:
Convert any hexadecimal number to octal number: ---------------------------------------------------- Input any 32-bit Hexadecimal Number: 5f The equivalant octal number is: 137
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C++ to convert hexadecimal number to binary number.
Next: Write a program in C++ to compare two numbers.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/cpp-exercises/for-loop/cpp-for-loop-exercise-81.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics