# Octal To Binary Conversion | C Program

## Octal to Binary Conversion in C

Here, in this page we will discuss about octal to binary conversion in C. There are 2 approaches of converting an octal number into a binary number like:

• Write 3 digit binary number for all the digits of a number from left to right.
• first convert octal to decimal and then decimal to binary .

We will discuss the algorithm for both approaches.

Example: Octal number is 374 = 011111100  is its equivalent in binary ## Algorithm for first approach:-

Step 1: Start

Step 2: Ask the user to enter an octal number as an input.

Step 3: Store the inserted number in the array octalnum.

Step 4: With the help of switch statement, evaluate every number of the octal number

Step 5: Print the equal binary value in a 3 digit number (Eg. 000)

Step 6: Do step 4 under the while loop.

Step 7: Stop

#### Binary Representation :

• 0 => 000
• 1 => 001
• 2 => 010
• 3 => 011
• 4 => 100
• 5 => 101
• 6 => 110
• 7 => 111   ## Program to convert octal number to binary number:

```/*
* C Program to Convert Octal to Binary number
*/
#include<stdio.h>#include<conio.h>

#define MAX 1000

int main()
{
char octalnum[MAX];    //For initialize
long i = 0;    //taking user input of octalnum
printf("Insert an octal number: ");
scanf("%s", octalnum);
printf("Equivalent binary number: ");
while (octalnum[i])
{        //use switch case for multiple condition
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;
//for invalid case           default:
printf("\n Invalid octal digit %c ", octalnum[i]);
return 0;
}
i++;
}
return 0;
}```

## Output

```Insert an octal number: a
Equivalent binary value:
Invalid octal digit a
Insert an octal number: 160
Equivalent binary value: 001110000```

## Algorithm for second approach:-

1. User gives an input(octal number).
2. Input is stored in an int type variable say octal.
3. A function convert() is called using octal as parameter.
1. A loop is started to convert octal into a decimal number
1. Digits of octal are extracted one by one starting from right.

int rem = octal%10;

2. Extracted digits are multiplied with proper base i.e. power of 8.

int res = rem*pow(8,i);

3. After multiplying each digit with proper base the results are added and stored in another variable say decimal.

decimal += res;

2. Another loop is started to convert decimal into a binary number

1. The number is divided by 2 and the remainder is stored.

rem = decimal%2;

2. The binary number is constructed using the remainder.

binary += rem*i;

3. The loop will run until the number is not equal to 0.

while (n!=0)

4. binary is returned and printed as output.

## Code in C

`    #include <stdio.h>    //Function to convert octal to binary    long convert(int octal)    {        int decimal = 0, i = 0;        long binary = 0;        //converting octal to decimal        while(octal != 0)        {            int rem = octal%10;            int res=rem * pow(8,i);            decimal += res;            i++;            octal/=10;        }        i = 1;        //converting decimal to binary        while (decimal != 0)        {            int rem = decimal % 2;            binary += rem * i;            decimal /= 2;            i *= 10;        }        return binary;    }    //main program    int main()    {        int octal;        printf( "Enter an octal number: ");        //user input        scanf("%d", &octal);        //function call        long binary = convert(octal);        //printing output        printf(" %d  in octal = %d in binary", octal, binary);        return 0;    }`
`Output :Enter an octal number: 7474 in octal = 111100 in binary`