C Program to Convert Octal to Binary

Problem: Write a program to convert octal number into binary.

Solution:
  1. Accept a octal number as input.
  2. Using switch statement and while loop display the binary value of each digit of a octal number.

C program to convert octal to binary

/* Aim: C Program to Convert Octal to Binary */

#include<stdio.h>
#define MAX 1000

int main()
{
	char octalnum[MAX];
	long i = 0;

	printf("Enter any octal number: ");
	scanf("%s", octalnum);

	printf("Equivalent binary value: ");
  
	while (octalnum[i])
	{
		switch (octalnum[i])
		{

		case '0':
			printf("000"); break;

		case '1':
			printf("001"); break;

		case '2':
			printf("010"); break;

		case '3':
			printf("011"); break;

		case '4':
			printf("100"); break;

		case '5':
			printf("101"); break;

		case '6':
			printf("110"); break;

		case '7':
			printf("111"); break;

		default:
			printf("\n Invalid octal digit %c ", octalnum[i]);

		return 0;
		}

		i++;

	}

	return 0;
}

/* Output of above code:- 

Enter any octal number: b
Equivalent binary value:
Invalid octal digit b
 
Enter any octal number: 160
Equivalent binary value: 001110000

*/
Get This Program: