C Program to Convert Octal to Binary

C Program to Convert Octal to Binary

Write C Program to Convert Octal to Binary

C Program to Convert Octal to Binary

#include <stdio.h>
#include <math.h>

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

	printf("Enter the Octal Number :--> ");
	scanf("%d", &n);

	int i = 0;

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

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

	printf("\nThe Binary Number is :--> ");
	for(k = j - 1; k >= 0; k--)
	{
		printf("%d",B[k]);
	}

	return 0;
}

Output:

Enter the Octal Number :--> 56

The Decimal Number is :--> 46
The Binary Number is :--> 101110