C++ Program to Convert Octal to Binary

C++ Program to Convert Octal to Binary

Write C++ Program to Convert Octal to Binary

// CPP Program to Convert Octal to Binary

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	int n, ans = 0, B[100];

	cout << "Enter the Octal Number :--> ";
	cin >> n;

	int i = 0;

	while(n != 0)
	{
		int digit = n % 10;
		ans = ans + (digit* pow(8,i));
		n =  n / 10;
		i++;
	}
	cout << "The Decimal Number is :--> " << ans;

	int j = 0, k;
	while(ans > 0)
	{
		B[j] = ans % 2;			//to store the remainder in array
		ans = ans >> 1;
		j++;
	}

	cout << "\nThe Binary Number is :--> ";
	for(k = j - 1; k >= 0; k--)
	{
		cout << B[k];
	}

	return 0;
}

Output:

Enter the Octal Number :--> 56
The Decimal Number is :--> 46
The Binary Number is :--> 101110