C Program to Convert Octal to Binary

Let’s understand how to Convert Octal Num to Binary in C Programming Language.

C Program to Convert Octal to Binary

#include <stdio.h>

/*Learn C Programming at CodeAtGlance.com*/

#include <math.h>

long octToBinaryConversion(int octalnum)

{

    int dec_number = 0, i = 0;

    long binarynum = 0;

    while(octalnum != 0)

    {

                dec_number = dec_number + (octalnum%10) * pow(8,i);

                i++;

                octalnum = octalnum / 10;

    }
    i = 1;
    while (dec_number != 0)

    {
                binarynum = binarynum + (dec_number % 2) * i;

                dec_number = dec_number / 2;

                i = i * 10;

    }
}

int main()
{
    int octalnum;

    printf("Enter an octal number: ");

    scanf("%d", &octalnum);

    printf("Equivalent binary number is: %ld", octToBinaryConversion(octalnum));

    return 0;
}
]]>

Leave a Reply

Shopping cart