C++ program to convert decimal to binary

Hello all, today we are going to write a C++ program to convert decimal to binary number, let’s first discuss how we can convert a decimal number to binary number and later we will write the program for the same. How Program works? Lets take an example of 11. Now divide it by 2 and … 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 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++