This is a C program to Convert Octal to Binary.
This program takes a octal number as input and converts it into binary.
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.
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.
/*
* 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;
}
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.
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
- Watch Advanced C Programming Videos
- Check C Books
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice BCA MCQs