C Program to Convert Binary Number to Octal and Vice-Versa

Binary and octal number systems are commonly used in computer science and digital electronics. Converting between these two systems is an essential skill. This article explores C Program to Convert Binary Number to Octal and Vice-Versa. We will discuss three different solutions for each conversion, complete with explanations, example code, and outputs.

Prerequisites

Before diving into the solutions, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with number systems (binary and octal).
  • A C compiler installed on your system (e.g., GCC).

Part 1: Binary to Octal Conversion

Solution 1: Direct Grouping Method

1.1 Explanation

In this approach, we group the binary digits into sets of three, starting from the right, and then convert each group to its corresponding octal digit.

1.2 Program

C
#include <stdio.h>
#include <string.h>
#include <math.h>

int binaryToOctal(char *binary) {
    int length = strlen(binary);
    int octal = 0, decimal = 0, power = 0;

    // Convert binary to decimal
    for (int i = length - 1; i >= 0; i--) {
        if (binary[i] == '1') {
            decimal += pow(2, power);
        }
        power++;
    }

    // Convert decimal to octal
    power = 1;
    while (decimal != 0) {
        octal += (decimal % 8) * power;
        decimal /= 8;
        power *= 10;
    }

    return octal;
}

int main() {
    char binary[100];
    printf("Enter a binary number: ");
    scanf("%s", binary);

    int octal = binaryToOctal(binary);
    printf("Octal equivalent: %d\n", octal);

    return 0;
}

1.3 Output

C
Enter a binary number: 110110
Octal equivalent: 66

Solution 2: Using Arrays for Conversion

2.1 Explanation

In this approach, we use arrays to store intermediate results during the conversion from binary to octal.

2.2 Program

C
#include <stdio.h>
#include <string.h>

void binaryToOctal(char *binary) {
    int length = strlen(binary);
    int remainder = length % 3;
    char paddedBinary[100] = "";
    char octal[100] = "";
    int octalIndex = 0;

    // Padding binary number to make its length a multiple of 3
    if (remainder == 1) {
        strcat(paddedBinary, "00");
    } else if (remainder == 2) {
        strcat(paddedBinary, "0");
    }
    strcat(paddedBinary, binary);

    // Convert every 3 binary digits to a single octal digit
    for (int i = 0; i < strlen(paddedBinary); i += 3) {
        int value = 0;
        if (paddedBinary[i] == '1') value += 4;
        if (paddedBinary[i + 1] == '1') value += 2;
        if (paddedBinary[i + 2] == '1') value += 1;
        octal[octalIndex++] = value + '0';
    }
    octal[octalIndex] = '\0';

    printf("Octal equivalent: %s\n", octal);
}

int main() {
    char binary[100];
    printf("Enter a binary number: ");
    scanf("%s", binary);

    binaryToOctal(binary);

    return 0;
}

Output

C
Enter a binary number: 101011
Octal equivalent: 53

Solution 3: Using Bitwise Operations

3.1 Explanation

This method uses bitwise operations to convert the binary number to an octal number, providing an efficient and direct approach.

3.2 Program

C
#include <stdio.h>
#include <string.h>

int binaryToDecimal(char *binary) {
    int decimal = 0, base = 1, length = strlen(binary);

    for (int i = length - 1; i >= 0; i--) {
        if (binary[i] == '1') {
            decimal += base;
        }
        base *= 2;
    }

    return decimal;
}

int decimalToOctal(int decimal) {
    int octal = 0, base = 1;

    while (decimal > 0) {
        octal += (decimal % 8) * base;
        decimal /= 8;
        base *= 10;
    }

    return octal;
}

void binaryToOctal(char *binary) {
    int decimal = binaryToDecimal(binary);
    int octal = decimalToOctal(decimal);

    printf("Octal equivalent: %d\n", octal);
}

int main() {
    char binary[100];
    printf("Enter a binary number: ");
    scanf("%s", binary);

    binaryToOctal(binary);

    return 0;
}

Output

C
Enter a binary number: 111010
Octal equivalent: 72