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++: Divide the original decimal number by 16. … Read more

Program to Display Size of various Data Types in C++

Write a Program to Display Size of various Data Types in C++ PROGRAM: #include<iostream> using namespace std; int main() { int x=25; cout<<"sizeof(char)tt"; cout<<sizeof(char)<<endl; cout<<"sizeof(signed char)t"; cout<<sizeof(signed char)<<endl; cout<<"sizeof(unsigned char)t"; cout<<sizeof(unsigned char)<<endl; cout<<"sizeof(int)tt"; cout<<sizeof(int)<<endl; cout<<"sizeof(signed int)t"; cout<<sizeof(signed int)<<endl; cout<<"sizeof(unsigned int)t"; cout<<sizeof(unsigned int)<<endl; cout<<"sizeof(short)tt"; cout<<sizeof(short)<<endl; cout<<"sizeof(signed short)t"; cout<<sizeof(signed short)<<endl; cout<<"sizeof(unsigned short)t"; cout<<sizeof(unsigned short)<<endl; cout<<"sizeof(short int)t"; cout<<sizeof(short int)<<endl; … Read more