Convert Octal to Hexadecimal in C++

In this post, firstly we will understand how to convert an Octal number to Hexadecimal number, then we will implement the same using C++ programming language. Let’s first understand how the conversion works: How to Convert Octal to Hexadecimal? When converting from octal to hexadecimal, it is often easier to first convert the octal number … Read more

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. For example: We want to convert hexadecimal number 65B2 to binary. For this we will replace each hexadecimal digit to binary values using the … Read more

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++