Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C++ Exercises: Convert a octal number to a hexadecimal number

C++ For Loop: Exercise-78 with Solution

Write a program in C++ to convert a octal number to a hexadecimal number.

Pictorial Presentation:

C++ Exercises: Convert a octal number to a hexadecimal number

Sample Solution:-

C++ Code :

#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
 
int main()
{
    int ar1[20],ar2[20],ar3[20];
    int h,i,j,k,l,x,fr,flg,rem,n1,n3;    
    float rem1,n2,n4,dno;
    char octal_num[20];
    x=fr=flg=rem=0;
    rem1=0.0;
	cout << "\n\n Convert any octal number to a hexadecimal number:\n";
	cout << "------------------------------------------------------\n";
	cout << " Input any octal number: ";
	cin>> octal_num;	
	
    for(i=0,j=0,k=0;i<strlen(octal_num);i++)
    {
        if(octal_num[i]=='.')
        {
            flg=1;
        }
        else if(flg==0)
            ar1[j++]=octal_num[i]-48;
        else if(flg==1)
            ar2[k++]=octal_num[i]-48;
    }
    x=j;
    fr=k;
    for(j=0,i=x-1;j<x;j++,i--)
    {
        rem = rem +(ar1[j] * pow(8,i));
    }
    for(k=0,i=1;k<fr;k++,i++)
    {
        rem1 = rem1 +(ar2[k] / pow(8,i));
    }
    rem1=rem+rem1;
    dno=rem1;
    n1=(int)dno;
    n2=dno-n1;
 
    i=0;
    while(n1!=0)
    {
        rem=n1 % 16;
        ar3[i] = rem;
        n1=n1 /16;
        i++;
    }
    j=0;
    while(n2!=0.0)
    {
        n2=n2 * 16;
        n3=(int)n2;
        n4=n2-n3;
        n2=n4;
        ar1[j]=n3;
        j++;
        if(j==4)
        {
            break;
        }
    }
    l=i;
    cout<<" The hexadecimal value of "<<octal_num<<" is: ";
    for(i=l-1;i>=0;i--)
    {
        if(ar3[i]==10)
           cout<<"A";
        else if(ar3[i]==11)
           cout<<"B";
        else if(ar3[i]==12)
           cout<<"C";
        else if(ar3[i]==13)
           cout<<"D";
        else if(ar3[i]==14)
           cout<<"E";
        else if(ar3[i]==15)
           cout<<"F";
        else
        cout<<ar3[i];
    }
    h=j;
    cout<<".";
    for(k=0;k<h;k++)
    {
        if(ar1[k]==10)
           cout<<"A";
        else if(ar1[k]==11)
           cout<<"B";
        else if(ar1[k]==12)
           cout<<"C";
        else if(ar1[k]==13)
           cout<<"D";
        else if(ar1[k]==14)
           cout<<"E";
        else if(ar1[k]==15)
           cout<<"F";
        else
        cout<<ar1[k];
    }
    cout<<endl;    
}

Sample Output:

 Convert any octal number to a hexadecimal number:                     
------------------------------------------------------                 
 Input any octal number: 77                                            
 The hexadecimal value of 77 is: 3F.

Flowchart:

Flowchart: Convert a octal number to a hexadecimal number

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C++ to convert a octal number to binary number.
Next: Write a program in C++ to convert a hexadecimal number to decimal number.

What is the difficulty level of this exercise?