C Program to Convert Octal to Binary

In this tutorial, we will learn about how to create a program in C that converts any given octal number by user at run-time into its equivalent binary value. At last we have also created a function-driven program that does the same job. In function-driven program, we have defined a function named OctToBin() that takes octal number as argument and converts it into its equivalent binary value.

But before going through the program, if you are not aware of

  • Octal Number
  • Binary Number
  • Octal to Binary Conversion Process

then refer to Octal to Binary conversion step by step process. Now let's move on to the program.

Octal to Binary in C

To convert octal number to binary number in C programming, you have to ask from user to enter octal value and convert it into binary value, then display the equivalent value in binary as output. The question is, Write a program in C that converts any given number (in octal number system) to its equivalent value in binary number system. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    int octnum, rev=0, rem;
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    while(octnum!=0)
    {
        rem = octnum%10;
        rev = (rev*10) + rem;
        octnum = octnum/10;
    }
    octnum = rev;
    printf("\nEquivalent Binary value = ");
    while(octnum!=0)
    {
        rem = octnum%10;
        switch(rem)
        {
            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(" InvalidOctalDigit(%d) ", rem);
                break;
        }
        octnum = octnum/10;
    }
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the sample run. This is the first snapshot:

c program convert octal to binary

Now supply any number say 734 as input and press ENTER key to see its value in Binary form as shown in the second snapshot of the sample run give here:

octal to binary in c

As the number 734 has 3 digits, that are 7, 3 and 4. Here 7 in binary is 111, 3 in binary is 011 and 4 in binary is 100. Therefore after combining all numbers in octal and binary, we have 734 (in octal) equals 111011100 (in binary) as shown in above snapshot. You can also write (734)8 = (111011100)2

Program Explained

  • Receive any number (in octal) as input say 734
  • Now reverse the number using while loop
  • Here we have reverse the octal number because while calculating remainder one by one, of given octal number, we will get last digit as first remainder and first digit as last remainder. For example, using modulous (%) operator to find digit or remainder of any number say 734. That is using 734%10, we will get 4 as first digit or remainder. And here 4 is the last digit of the number 734. Therefore we have reverse the number first
  • Now initialize the reverse of the number stored in rev variable to the octnum variable
  • Create a while loop that runs until the value of octnum becomes 0
  • At first run, the condition of the while loop, octnum!=0 or 437!=0 (as we have reverse the number 734) evaluates to be true, therefore program flow goes inside the loop
  • And octnum%10 or 437%10 or 7 (first digit of 734) gets initialized to rem
  • Create switch() case, to check what the value present inside rem variable
  • Whatever the value present in rem variable, print its equivalent binary value. For example, if rem holds 7, then binary equivalent of 7 gets printed as output, and after printing binary equivalent of rem, octnum/10 or 437/10 or 43 gets initialized to octnum and the program flow goes back to the condition of the while loop
  • There, the condition octnum!=0 or 43!=0 evaluates to be true again, therefore program flow goes inside the loop
  • And octnum%10 or 43%10 or 3 gets initialized to rem, and then using switch() case, binary equivalent of 3 gets printed as output, and continue to do the similar operation as told above, until the value of octnum becomes 0
  • In this way, binary equivalent of given octal number gets printed on output screen

Octal to Binary in C using strcat() Function

Let's create the same purpose program in another way. Here we have used the string function named strcat() of string.h library, to concatenate the string one by one. We have stored binary value in string format as shown in the program given below. The question is, Write a program in C that converts octal number to its equivalent binary number using library function strcat(). The answer to this question is:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    int octnum, rev=0, rem, count=0;
    char binnum[40] = "";
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    while(octnum!=0)
    {
        rem = octnum%10;
        if(rem>7)
        {
            count++;
            break;
        }
        rev = (rev*10) + rem;
        octnum = octnum/10;
    }
    if(count==0)
    {
        octnum = rev;
        printf("\nEquivalent Binary value = ");
        while(octnum!=0)
        {
            rem = octnum%10;
            switch(rem)
            {
                case 0: strcat(binnum, "000");
                    break;
                case 1: strcat(binnum, "001");
                    break;
                case 2: strcat(binnum, "010");
                    break;
                case 3: strcat(binnum, "011");
                    break;
                case 4: strcat(binnum, "100");
                    break;
                case 5: strcat(binnum, "101");
                    break;
                case 6: strcat(binnum, "110");
                    break;
                case 7: strcat(binnum, "111");
                    break;
            }
            octnum = octnum/10;
        }
        printf("%s", binnum);
    }
    else
        printf("\nInvalid Octal Digit %d", rem);
    getch();
    return 0;
}

Here is the final snapshot of the above program's sample run:

octal to binary conversion c

As octal digit can not exceed the value greater than 7, therefore we have used count variable to check whether any digit of the given octal number exceeds the value 7 or not. If it exceeds, then its value gets incremented and then exits from the current loop. And before converting octal to binary, we have checked whether count holds its original value or not. If it holds, then all the octal digit is correct, otherwise print any message like Invalid Octal digit was entered by you.

Octal to Binary in C using Indirect Conversion

The question is, Write a program in C that converts octal number to its equivalent binary number in such way that the given octal number first converted into decimal number and then decimal number gets converted into binary number. The answer to this question is:

#include<stdio.h>
#include<conio.h>
int main()
{
    int octnum, decnum=0, binnum[40], rem, mul=1, i=0, count=0;
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    while(octnum!=0)
    {
        rem = octnum%10;
        if(rem>7)
        {
            count++;
            break;
        }
        decnum = decnum + (rem*mul);
        mul = mul*8;
        octnum = octnum/10;
    }
    if(count==0)
    {
        while(decnum!=0)
        {
            binnum[i] = decnum%2;
            i++;
            decnum = decnum/2;
        }
        printf("\nEquivalent Binary Value = ");
        for(i=(i-1); i>=0; i--)
            printf("%d", binnum[i]);
    }
    else
        printf("\nInvalid Octal Digit %d", rem);
    getch();
    return 0;
}

Here is the final snapshot of the above program's sample run:

octal to binary program c

Octal to Binary in C using User-defined Function

To convert octal to binary using user-defined function named OctToBin(). Create this function in a way that it takes one argument that will be given octal number by user.

Here we have declared all the common variables (present inside both the function main() and OctToBin()) as global variable to make it known through both the function. The variable i and count gets declared as static variable, so that they can remember their previous value. As we all knows that the static variable holds 0 as its initial value automatically. Therefore here we have not initialized zero to both the static variable i and count.

#include<stdio.h>
#include<conio.h>
void OctToBin(int oct);
static int i, count;
int bin[40];
int main()
{
    int octnum;
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    OctToBin(octnum);
    if(count==0)
    {
        printf("\nEquivalent Binary Value = ");
        for(i=(i-1); i>=0; i--)
            printf("%d", bin[i]);
    }
    else
        printf("\nYou've entered invalid Octal Digit");

    getch();
    return 0;
}
void OctToBin(int oct)
{
    int dec=0, rem, mul=1;
    while(oct!=0)
    {
        rem = oct%10;
        if(rem>7)
        {
            count++;
            break;
        }
        dec = dec + (rem*mul);
        mul = mul*8;
        oct = oct/10;
    }
    if(count==0)
        while(dec!=0)
        {
            bin[i] = dec%2;
            i++;
            dec = dec/2;
        }
}

Here is the final snapshot of the sample run:

c octal to binary using function

Same Program in Other Languages

C Online Test


« Previous Program Next Program »