Q:

C++ Program to Convert Binary Number to Octal

belongs to collection: C++ Number Solved Programs

0

Write a C++ Program to Convert Binary Number to Octal. Here’s simple C++ Program to Convert Binary Number to Octal in C++ Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C++ Data Types.

 
 

Here is source code of the C++ Program to Convert Binary Number to Octal. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :

/* C++ Program to Convert Binary Number to Octal  */

#include <iostream>
#include <cmath>

using namespace std;

int convertBinarytoOctal(long long);
int main()
{
    long long binaryNumber;

    cout << "Enter a binary number: ";
    cin >> binaryNumber;

     cout <<"\n [ "<< binaryNumber << " ] in binary = [ " << convertBinarytoOctal(binaryNumber) << " ] in octal \n";

    return 0;
}

int convertBinarytoOctal(long long binaryNumber)
{
    int octalNumber = 0, decimalNumber = 0, i = 0;

    while(binaryNumber != 0)
    {
        decimalNumber += (binaryNumber%10) * pow(2,i);
        ++i;
        binaryNumber/=10;
    }

    i = 1;

    while (decimalNumber != 0)
    {
        octalNumber += (decimalNumber % 8) * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}

OUTPUT : :


/* C++ Program to Convert Binary to Octal  */

Enter a binary number: 100111

 [ 100111 ] in binary = [ 47 ] in octal

Process returned 0

Above is the source code for C++ Program to Convert Binary to Octal which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Number Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program to Convert Octal Number to Binary... >>
<< C++ Program to find Prime Numbers Between Two Inte...