C Program to Convert Octal to Binary

This is a C program to Convert Octal to Binary.

Problem Description

This program takes a octal number as input and converts it into binary.

Problem Solution

1. Take a octal number as input.
2. Print the binary value of each digit of a octal number. Use switch statement and while loop to do this.

Program/Source Code

Here is source code of the C program to Convert Octal to Binary. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Convert Octal to Binary
  3.  */
  4. #include <stdio.h>
  5. #define MAX 1000
  6.  
  7. int main()
  8. {
  9.     char octalnum[MAX];
  10.     long i = 0;
  11.  
  12.     printf("Enter any octal number: ");
  13.     scanf("%s", octalnum);
  14.     printf("Equivalent binary value: ");
  15.     while (octalnum[i])
  16.     {
  17.         switch (octalnum[i])
  18.         {
  19.         case '0':
  20.             printf("000"); break;
  21.         case '1':
  22.             printf("001"); break;
  23.         case '2':
  24.             printf("010"); break;
  25.         case '3':
  26.             printf("011"); break;
  27.         case '4':
  28.             printf("100"); break;
  29.         case '5':
  30.             printf("101"); break;
  31.         case '6':
  32.             printf("110"); break;
  33.         case '7':
  34.             printf("111"); break;
  35.         default:
  36.             printf("\n Invalid octal digit %c ", octalnum[i]);
  37.             return 0;
  38.         }
  39.         i++;
  40.     }
  41.     return 0;
  42. }
Program Explanation

1. Take a octal number as input and store it in the array octalnum.
2. Using switch statement access each digit of a octal number and print its equivalent binary value in a 3 bit fashion. For example: for 0, print its binary value as 000.
3. Do step 2 under a while loop.
4. Exit.

advertisement
advertisement
Runtime Test Cases
Output:
 
Enter any octal number: a
Equivalent binary value:
Invalid octal digit a
 
 
Enter any octal number: 160
Equivalent binary value: 001110000

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.