C Program To Convert Octal to Binary Number

C program to convert Octal to binary number; In this tutorial, i am going to show you how to convert octal numbers to binary numbers in c program with the help of while loop and function.

C Program To Convert Octal to Binary Number

  • C Program to Convert Octal to Binary Number using While Loop
  • C Program to Convert Octal to Binary Number using Function

C Program to Convert Octal to Binary Number using While Loop

#include <stdio.h>
#include <math.h>
int main()
{
    int i, octal, decimal = 0;
    long binary = 0;
    i = 0;
    
    printf("Enter the Octal Number = ");
    scanf("%d",&octal);
    while(octal != 0)
    {
        decimal = decimal + (octal % 10) * pow(8, i);
        i++;
        octal = octal / 10;
    }
    i = 1;
    while(decimal != 0)
    {
        binary += ((decimal % 2) * i);
        decimal = decimal / 2;
        i = i * 10;
    }
    printf("The Binay Value = %ld\n", binary); 
}

The result of the above c program; as follows:

Enter the Octal Number = 1025
The Binay Value = 1000010101

C Program to Convert Octal to Binary Number using Function

#include <stdio.h>
#include <math.h>
long octalToBinary(int octal)
{
    int i, decimal = 0;
    long binary = 0;
    for (i = 0; octal != 0; i++)
    {
        decimal = decimal + (octal % 10) * pow(8, i);
        octal = octal / 10;
    }
    for (i = 1; decimal != 0; i = i * 10)
    {
        binary = binary + (decimal % 2) * i;
        decimal = decimal / 2;
    }
    return binary;
}
int main()
{
    int octal;
    printf("Enter the Octal Number = ");
    scanf("%d", &octal);
    printf("The Decimal Value = %ld\n", octalToBinary(octal));
}

The result of the above c program; as follows:

Enter the Octal Number = 1256
The Decimal Value = 1010101110

More C Programming Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.