CPP Program convert binary to octal
In this program, you will learn about the conversion of binary numbers to octal numbers in the cpp programming language.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long long binaryNumber,original;
cout << " Enter a binary number ";
cin >> binaryNumber;
original=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;
}
cout << original << " in binary " << octalNumber<< " in octal "<<endl;
return 0;
}
Output:
Enter a binary number 1111
1111 in binary 17 in octal
In the above program,first convert binary number to decimal then convert decimal to octal number .Two while loops are used in the code for conversion.
Comments
Post a Comment