Unleashing the Power of C Programming: Binary to Octal Conversion

The Binary to Octal Conversion Process

To convert a binary number to octal, we need to take a two-step approach. First, we’ll convert the binary number to decimal, and then we’ll convert the decimal number to octal. This may seem complex, but it’s worth the effort.

Program to Convert Binary to Octal

Take a look at the following program that brings this concept to life:


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

int binaryToDecimal(char binary[]) {
    int decimal = 0, base = 1;
    int len = strlen(binary);

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

    return decimal;
}

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

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

    return octal;
}

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

    int decimal = binaryToDecimal(binary);
    int octal = decimalToOctal(decimal);

    printf("The octal equivalent is: %o\n", octal);

    return 0;
}

The output of this program will be an octal number equivalent to the input binary number.

The Reverse: Converting Octal to Binary

Now, let’s flip the script and explore the process of converting an octal number to binary. This time, we’ll first convert the octal number to decimal, and then we’ll convert the decimal number to binary.

Program to Convert Octal to Binary

Here’s the program that makes it happen:


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

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

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

    return decimal;
}

char* decimalToBinary(int decimal) {
    int binaryNum[32];
    int i = 0;
    while (decimal > 0) {
        binaryNum[i] = decimal % 2;
        decimal /= 2;
        i++;
    }

    char* binary = (char*)malloc((i + 1) * sizeof(char));
    for (int j = i - 1; j >= 0; j--)
        binary[i - j - 1] = (binaryNum[j] == 1)? '1' : '0';
    binary[i] = '\0';

    return binary;
}

int main() {
    int octal;
    printf("Enter an octal number: ");
    scanf("%d", &octal);

    int decimal = octalToDecimal(octal);
    char* binary = decimalToBinary(decimal);

    printf("The binary equivalent is: %s\n", binary);

    free(binary);

    return 0;
}

The output of this program will be a binary number equivalent to the input octal number.

Unlocking the Secrets of Number Systems

By mastering these two programs, you’ll gain a deeper understanding of the relationships between binary, decimal, and octal number systems. This knowledge will serve as a powerful tool in your C programming journey, enabling you to tackle complex problems with confidence.

  • Binary number system: A base-2 number system that uses only two digits: 0 and 1.
  • Octal number system: A base-8 number system that uses eight digits: 0 through 7.
  • Decimal number system: A base-10 number system that uses ten digits: 0 through 9.

So, take the first step today and start exploring the fascinating world of number systems!

Leave a Reply