Convert Hexadecimal to Octal Program in C++

Google 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

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on hitesh.xc@gmail.com or 8076671483

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

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483