Convert given Hexadecimal Number to Octal in C++

In, this post we will learn How to convert given Hexadecimal Number to octal.

First of all, let’s see what these numbers are,

It is a number system that consists of 16 elements. this number system contains 10 elements from 0 to 9 and 6 alphabets from A to F . all the other numbers can be easily represented in the hexadecimal number system with the help of the combination of these 16 elements. due to that, the hexadecimal number system is also known as “Base-16” number system.

Eg.:

What is the Octal number?
It is a number system that consists of 8 elements, from 0 to 7. All other numbers can be represented in the octal number system. due to that, the octal number system is referred to as ” Base-8″ number system.

Eg.:

Steps to convert Hexadecimal to Octal in C++

the main aim in this section is to break the problem into small problems and then solve them.

• First of all, Take input of the hexadecimal number.
• to create a character array.
• The character array must contain the coded numbers of the hexadecimal form  ‘0-F’
• Traverse through the hexadecimal number and find it in character array.
• If the array contains the character then identify that index number.
• the index number will correspond to its value.
• calculate the decimal value from the index.
• Convert the generated decimal value to its corresponding octal number.
• now, print this array in reverse order.
• as a result, you will get an octal number.

This method of conversion of the Hexadecimal to Octal is more complex but very simple in implementation for the beginners.

You may also learn,

```#include <bits/stdc++.h>
using namespace std;

int main()
{
string hex;
cin>>hex;
char ch[]= {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int l = hex.length();
int t=((4*l)/3)+1;
int sumd=0,k=0,a[t]={0};

for (int i=l-1;i>=0;i--)
{
for(int j=0;j<16;j++)
{
if(hex[i]==ch[j])
{

sumd+=j*pow(16,k);

}

}
k++;
}
int m=0;
while(sumd!=0)
{
a[m]=sumd%8;
sumd=sumd/8;
m++;
}
for (int i=t-1;i>=0;i--)
{
cout<<a[i];
}

}
```