Convert number from octal to binary C program

Convert number from octal to binary. Octal number is a base 8 number and octal number contains 0,1,2,3,4,5,6,7 digits and binary number is a base 2 number and it contains 0 and 1.

In octal system digits 0 to 7 are written as

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

So if octal number is 345 then binary number is
3 = 011;  4 = 100;  5 = 101;
so 345 in binary system is 011100101

C Program:

/* C program to convert octal to binary */
#include<stdio.h>
int main()
{
 char octal[100];
 
 printf("Enter octal number: ");
 scanf("%s",octal);
 
 printf("%s in binary: \n",octal);
 
 int i=0;
 
 while(octal[i])
 {
 
 
  switch(octal[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; 
  }
 
  i++;
 }
 
 return 0;
}


Popular posts from this blog