C Program to Convert Binary Numbers to Octal and vice-versa
Example 1: Program to Convert Binary to Octal
In this program, we will first convert binary numbers to decimals. Then, the decimal number is converted to an octal.
#include <stdio.h>
#include <math.h>
int convertBinarytoOctal(long long binaryNumber);
int main()
{
long long binaryNumber;
printf("Enter a binary number: ");
scanf("%lld", &binaryNumber);
printf("%lld in binary = %d in octal", binaryNumber, convertBinarytoOctal(binaryNumber));
return 0;
}
int convertBinarytoOctal(long long binaryNumber)
{
int octalNumber = 0, decimalNumber = 0, i = 0;
while(binaryNumber != 0)
{
decimalNumber += (binaryNumber%10) * pow(2,i);
++i;
binaryNumber/=10;
}
i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
Output
Enter a binary number: 101001 101001 in binary = 51 in octal
Example 2: Program to Convert Octal to Binary
In this program, the octal number to decimal to decimal at first. Then, the decimal number is converted to binary number.
#include <stdio.h>
#include <math.h>
long long convertOctalToBinary(int octalNumber);
int main()
{
int octalNumber;
printf("Enter an octal number: ");
scanf("%d", &octalNumber);
printf("%d in octal = %lld in binary", octalNumber, convertOctalToBinary(octalNumber));
return 0;
}
long long convertOctalToBinary(int octalNumber)
{
int decimalNumber = 0, i = 0;
long long binaryNumber = 0;
while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}
i = 1;
while (decimalNumber != 0)
{
binaryNumber += (decimalNumber % 2) * i;
decimalNumber /= 2;
i *= 10;
}
return binaryNumber;
}
Output
Enter an octal number: 67 67 in octal = 110111 in binary
example C program to convert binary numbers to octal and vice-versa:
#include <stdio.h>
#include <math.h>
// Function to convert binary to decimal
int binaryToDecimal(int binary) {
int decimal = 0, i = 0;
while (binary != 0) {
int remainder = binary % 10;
binary /= 10;
decimal += remainder * pow(2, i);
i++;
}
return decimal;
}
// Function to convert decimal to octal
int decimalToOctal(int decimal) {
int octal = 0, i = 0;
while (decimal != 0) {
int remainder = decimal % 8;
decimal /= 8;
octal += remainder * pow(10, i);
i++;
}
return octal;
}
// Function to convert binary to octal
int binaryToOctal(int binary) {
int decimal = binaryToDecimal(binary);
int octal = decimalToOctal(decimal);
return octal;
}
// Function to convert octal to decimal
int octalToDecimal(int octal) {
int decimal = 0, i = 0;
while (octal != 0) {
int remainder = octal % 10;
octal /= 10;
decimal += remainder * pow(8, i);
i++;
}
return decimal;
}
// Function to convert octal to binary
int octalToBinary(int octal) {
int decimal = octalToDecimal(octal);
int binary = 0, i = 0;
while (decimal != 0) {
int remainder = decimal % 2;
decimal /= 2;
binary += remainder * pow(10, i);
i++;
}
return binary;
}
int main() {
int choice, binary, octal;
printf("Enter 1 to convert binary to octal\n");
printf("Enter 2 to convert octal to binary\n");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter a binary number: ");
scanf("%d", &binary);
octal = binaryToOctal(binary);
printf("Octal number: %d\n", octal);
} else if (choice == 2) {
printf("Enter an octal number: ");
scanf("%d", &octal);
binary = octalToBinary(octal);
printf("Binary number: %d\n", binary);
} else {
printf("Invalid choice\n");
}
return 0;
}
In this program, we define functions to convert binary numbers to decimal and decimal numbers to octal, and functions to convert octal numbers to decimal and decimal numbers to binary. We then use these functions to convert binary numbers to octal and vice-versa.
The binaryToDecimal()
function takes a binary number as input and returns the equivalent decimal number. The decimalToOctal()
function takes a decimal number as input and returns the equivalent octal number.
The octalToDecimal()
function takes an octal number as input and returns the equivalent decimal number. The octalToBinary()
function takes an octal number as input and returns the equivalent binary number.
The binaryToOctal()
function first converts the binary number to decimal using the binaryToDecimal()
function, and then converts the decimal number to octal using the decimalToOctal()
function.