CPP Program convert octal to binary
In this example, you will learn about the conversion of octal to binary number in the cpp programming language .you must have knowledge about the following topics to understand the working of the program.
Following is the code of conversion.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int octalNumber,original;
cout << "Enter an octal number: ";
cin >> octalNumber;
original=octalNumber;
int decimalNumber = 0, i = 0;
long long binaryNumber = 0;
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}
i = 1;
while (decimalNumber != 0)
{
binaryNumber += (decimalNumber % 2) * i;
decimalNumber /= 2;
i *= 10;
}
cout << original << " in octal = " << binaryNumber << " in binary";
return 0;
}
Output:
Enter an octal number: 12
12 in octal = 1010in binary
Comments
Post a Comment