C program to convert binary to octal


Levels of difficulty: / perform operation:

Octal to binary conversion method:
To convert or change the octal number to binary number replace the each octal digits by a binary number using octal to binary chart.

Octal
Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111



Octal to binary conversion examples:
For example we want to convert or change octal number 65201 to decimal. For this we will replace each octal digit to binary values using the above table:
Octal number: 6 5 2 0 1
Binary values: 110 101 010 000 001

So (65201)8 = (110101010000001)2





C Program

#include<stdio.h>
#define MAX 1000
int main() {
	char octalNumber[MAX];
	long int i=0;
	printf("Enter any octal number: ");
	scanf("%s",octalNumber);
	printf("Equivalent binary value: ");
	while(octalNumber[i]) {
		switch(octalNumber[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("\nInvalid octal digit %c ",octalNumber[i]);
			return 0;
		}
		i++;
	}
	return 0;
}

Output

Enter any octal number: 123
Equivalent binary value: 001010011





Quantitative Aptitude
Reasoning
Programming
Interview