Convert Hexadecimal to Octal Program in C++

Advertisements

Prev Tutorial Next Tutorial

C++ program to Convert Hexadecimal to Octal

To convert Hexadecimal number to Octal number in C++ programming, We have to ask to the user to enter the hexadecimal number to convert it into Decimal number to display the equivalent value in Octal format on screen.
At first convert each digit in hexadecimal to four bit binary number and than convert binary to octal. For example convert (A4)16 to octal.

Example

Input Hexadecimal Number : 2C
Euivalent Octal Number : 54

Input Hexadecimal Number : 1E
Euivalent Octal Number : 36

Input Hexadecimal Number : D
Euivalent Octal Number : 15

C++ program to convert Hexadecimal to Octal

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>

void main ()
{
 
    long i,y,hexa[20],binary[20],rem,octal=0,z=0,temp=1,w=0;
    char input[20];
	clrscr();
    cout<<"Enter any Hexadecimal Number: ";
    gets(input);
    for(y=0;input[y]!='\0';y++);
    for(i=y-1;i>=0;i--)
    {
        if(input[i]>='A'&&input[i]<='F')
            hexa[i]=input[i]-55;
        else if(input[i]>='a')
            hexa[i]=input[i]-87;
        else
            hexa[i]=input[i]-48;
        for(int y=0;y<4;y++)
        {
            rem=hexa[i]%2;
            hexa[i]/=2;
            binary[z++]=rem;
        }
    }
    for(i=0;i<z;)
    {
        for(y=0;y<3;y++)
        {
            if(i>=0)
             octal+=binary[i++]*pow(2,y);
        }
       w+=octal*temp;
       octal=0;
       temp*=10;
    }
    cout<<"Equivalent Octal Number is: "<<w;

   getch();
}

Output

Enter any Hexadecimal Number: 1B
Equivalent Decimal Value is: 33
c++ program to convert hexadecimal to octal
Prev Tutorial Next Tutorial

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Advertisements


Buy Websites 9999595223

Buy Fresh and Payment Receive Media.net Account with Website. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs