Program to convert Decimal to Octal in C++

Here we will write a program to convert Decimal to Octal in C++ programming language. First lets start with how to convert any decimal number to an octal number: Example: Here is an example of using repeated division to convert 1792 from decimal number to an octal number: Decimal Number   Operation   Quotient   Remainder   Octal Result … Read more

Program to Convert Hexadecimal to Binary C++

How to convert Hexadecimal to Binary C++:

To convert or change the hexadecimal number to binary number replace the each octal digits by a binary number using hexadecimal to binary chart.

convert hexadecimal to binary C++

For example:

We want to convert hexadecimal number 65B2 to binary. For this we will replace each hexadecimal digit to binary values using the above table:

Hexadecimal number:  6    5    B    2

Binary values:      (0110) (0101) (1011) (0010)

So (65B2)16 = (0110010110110010)2

Download Code:  

[adinserter block=”1″]

Download: hextobin.cpp

Program to Convert Hexadecimal to Binary C++:

#include<iostream>

using namespace std;
#define MAX 1000

int main()
{
    char binaryNumber[MAX],hexaDecimal[MAX];
    long int i=0;

    cout<<"Enter any hexadecimal number: ";
    cin>>hexaDecimal;

    cout<<"nEquivalent binary value: ";
    while(hexaDecimal[i])
    {
         switch(hexaDecimal[i])
         {
             case '0': cout<<"0000"; break;
             case '1': cout<<"0001"; break;
             case '2': cout<<"0010"; break;
             case '3': cout<<"0011"; break;
             case '4': cout<<"0100"; break;
             case '5': cout<<"0101"; break;
             case '6': cout<<"0110"; break;
             case '7': cout<<"0111"; break;
             case '8': cout<<"1000"; break;
             case '9': cout<<"1001"; break;
             case 'A': cout<<"1010"; break;
             case 'B': cout<<"1011"; break;
             case 'C': cout<<"1100"; break;
             case 'D': cout<<"1101"; break;
             case 'E': cout<<"1110"; break;
             case 'F': cout<<"1111"; break;
             case 'a': cout<<"1010"; break;
             case 'b': cout<<"1011"; break;
             case 'c': cout<<"1100"; break;
             case 'd': cout<<"1101"; break;
             case 'e': cout<<"1110"; break;
             case 'f': cout<<"1111"; break;
             default:  cout<<"nInvalid hexadecimal digit "<<hexaDecimal[i];
         }
         i++;
    }

    return 0;
}

 

OUTPUT:

convert hexadecimal to binary c++

Program to Convert Octal to Hexadecimal C++

How to Convert Octal to Hexadecimal

When converting from octal to hexadecimal, it is often easier to first convert the octal number into binary and then from binary into hexadecimal. For example, to convert 345 octal into hex:

Octal  =345
Binary =011100101= 011100101 binary

Drop any leading zeros or pad with leading zeros to get groups of four binary digits (bits):
Binary 011100101 = 1110 0101

Then, look up the groups in a table to convert to hexadecimal digits.

Binary:00000001001000110100010101100111
Hexadecimal:01234567
Binary:10001001101010111100110111101111
Hexadecimal:89ABCDEF

 

Binary =11100101
Hexadecimal =E5= E5 hex

Therefore, through a two-step conversion process, octal 345 equals binary 011100101 equals hexadecimal E5.

C++ Program to Convert Octal to HexaDecimal:

#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;

int main()
{
    int a[20],b[20],c[20],rev[20];
    int h,i,j,k,l,x,fra,flag,rem,num1,num3;
    float rem1,num2,num4,dno;
    char s[20];
    x=fra=flag=rem=0;
    rem1=0.0;
    cout<<"ENTER THE OCTAL NUMBER : ";
    cin>>s;
    for(i=0,j=0,k=0;i<strlen(s);i++)
    {
        if(s[i]=='.')
        {
            flag=1;
        }
        else if(flag==0)
            a[j++]=s[i]-48;
        else if(flag==1)
            b[k++]=s[i]-48;
    }
    x=j;
    fra=k;
    for(j=0,i=x-1;j<x;j++,i--)
    {
        rem = rem +(a[j] * pow(8,i));
    }
    for(k=0,i=1;k<fra;k++,i++)
    {
        rem1 = rem1 +(b[k] / pow(8,i));
    }
    rem1=rem+rem1;
    dno=rem1;
    num1=(int)dno;
    num2=dno-num1;

    i=0;
    while(num1!=0)
    {
        rem=num1 % 16;
        rev[i] = rem;
        num1=num1 /16;
        i++;
    }
    j=0;
    while(num2!=0.0)
    {
        num2=num2 * 16;
        num3=(int)num2;
        num4=num2-num3;
        num2=num4;
        a[j]=num3;
        j++;
        if(j==4)
        {
            break;
        }
    }
    l=i;
    cout<<"nTHE HEXADECIMAL VALUE OF GIVEN OCTAL NO IS : ";
    for(i=l-1;i>=0;i--)
    {
        if(rev[i]==10)
           cout<<"A";
        else if(rev[i]==11)
           cout<<"B";
        else if(rev[i]==12)
           cout<<"C";
        else if(rev[i]==13)
           cout<<"D";
        else if(rev[i]==14)
           cout<<"E";
        else if(rev[i]==15)
           cout<<"F";
        else
        cout<<rev[i];
    }
    h=j;
    cout<<".";
    for(k=0;k<h;k++)
    {
        if(a[k]==10)
           cout<<"A";
        else if(a[k]==11)
           cout<<"B";
        else if(a[k]==12)
           cout<<"C";
        else if(a[k]==13)
           cout<<"D";
        else if(a[k]==14)
           cout<<"E";
        else if(a[k]==15)
           cout<<"F";
        else
        cout<<a[k];
    }
    return 0;
}

 

OUTPUT:

convert Octal to Hexadecimal

Program to convert Octal to Decimal C++

How Program Works:

To convert an octal number to decimal number:

  • multiply each digits separately of octal number from right side by  8^0,8^1,8^2,8^3 … respectively and then find out the sum of them.

For Example we want to convert octal number 3401 to decimal number.

i)  1 * 8 ^0 = 1*1 =1

ii)  0 * 8 ^1 = 0*8 =0

iii)  4 * 8 ^2 = 4*64 =256

iv)  3 * 8 ^3 = 3*512 =1536

v)  Sum= 1 + 0 + 256 + 1536 = 1793

  • So, (3401)8 = (1793)10

Program to convert Octal to Decimal C++:

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

using namespace std;

int main()
{
    long int octal,decimal =0;
    int i=0;

    cout<<"Enter any octal number: ";
    cin>>octal;
    cout<<endl;
    while(octal!=0)
    {
         decimal = decimal + (octal % 10) * pow(8,i++);
         octal = octal/10;
    }

    cout<<"Equivalent decimal value: "<<decimal<<endl;

   return 0;
}

 

OUTPUT:

octal to decimal c++

Program to convert Octal to Binary C++

Programming Logic:

To convert or change the octal number to binary number replace the each octal digits by a binary number using octal to binary chart.

octal to binary c++

 

For example take 25: Binary of 2 is 010 and Binary of 5 is 101.

Therefore binary of octal 25 is: 010101

Program to convert Octal to Binary C++:

#include<iostream>
using namespace std;

#define MAX 1000

int main()
{

    char octalNumber[MAX];
    long int i=0;

    cout<<"Enter any octal number: ";
    cin>>octalNumber;

    cout<<"Equivalent binary value: ";
    while(octalNumber[i])
    {
        switch(octalNumber[i])
    {
             case '0': cout<<"000";
             break;
             case '1': cout<<"001";
         break;
             case '2': cout<<"010";
         break;
             case '3': cout<<"011";
         break;
             case '4': cout<<"100";
         break;
             case '5': cout<<"101";
         break;
             case '6': cout<<"110";
         break;
             case '7': cout<<"111";
         break;
             default:  cout<<"nInvalid octal digit "<<octalNumber[i];
         return 0;
        }
        i++;
    }

    return 0;
}

 

OUTPUT:

octal to binary c++

Program to convert Decimal to Hexadecimal C++

Decimal Number System: 

It is base 10 number system which uses the digits from 0 to 9.

Hexadecimal Number System:

It is base 16 number system which uses the digits from 0 to 9 and A, B, C, D, E, F.

Logic to Convert Decimal to Hexadecimal C++:

  1. Divide the original decimal number by 16.
  2. Divide the quotient by 16.
  3. Repeat the step 2 until we get quotient equal to zero.
  4. Equivalent Hexadecimal number would be the remainders of each step in the reverse order.

PROGRAM:

#include<iostream>
using namespace std;

int main()
{
    long int decimalNumber,remainder,quotient;
    int i=1,j,temp;
    char hexadecimalNumber[100];

    cout<<"Enter any decimal number: ";
    cin>>decimalNumber;

    quotient = decimalNumber;

    while(quotient!=0)
    {
         temp = quotient % 16;

      //To convert integer into character
      if( temp < 10)
           temp =temp + 48;
      else
         temp = temp + 55;

      hexadecimalNumber[i++]= temp;
      quotient = quotient / 16;
    }
    cout<<"nEquivalent hexadecimal number of "<<decimalNumber<<" is : ";
    for(j = i -1 ;j> 0;j--)
      cout<<hexadecimalNumber[j];
    cout<<endl;
    return 0;
}

How Code Works:

Let user enter 900.

  • 900 / 16  Remainder : 4 , Quotient : 56
  •  56 / 16  Remainder : 8 , Quotient : 3
  •  3 / 16  Remainder : 3 , Quotient : 0

Now Hexadecimal Number is remainders in each step in reverse order i.e. 384

Hence, hexadecimal equivalent of 900 is 384.

OUTPUT:

decimal to hexadecimal C++