C program to convert Octal to binary number; Through this tutorial, we will learn how to convert octal numbers to binary numbers in c program using while loop and function.
C Program To Convert Octal to Binary Number
Let’s use the following programs to convert octal numbers to binary numbers in c program using while loop and function:
- 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 output 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 output of the above c program; as follows:
Enter the Octal Number = 1256 The Decimal Value = 1010101110