Algorithm


Binary to Octal Conversion Algorithm:

    -Input: Accept a binary number as input.
    -Initialize: Set variables octalNumber and decimalNumber to 0, and i to 0.
    -Binary to Decimal Conversion:
        - While the binary number is not zero:
           - Extract the last digit of the binary number.
           - Multiply the digit by 2 2i and add it to decimalNumber.
           - Increment i.
           - Divide the binary number by 10.
    Decimal to Octal Conversion:
        -Reset i to 1.
        -While the decimal number is not zero:
            -Extract the last digit of the decimal number.
            -Multiply the digit by i and add it to octalNumber.
            -Increment i by a factor of 10.
            -Divide the decimal number by 8.
    Output: The variable octalNumber contains the octal equivalent.

Octal to Binary Conversion Algorithm:

    Input: Accept an octal number as input.
     -Initialize: Set variables binaryNumber and decimalNumber to 0, and i to 0.
     -Octal to Decimal Conversion:
        -While the octal number is not zero:
            -Extract the last digit of the octal number.
            -Multiply the digit by 8 8i and add it to decimalNumber.
            -Increment i.
            -Divide the octal number by 10.
    Decimal to Binary Conversion:
        -Reset i to 1.
        -While the decimal number is not zero:
            -Extract the last digit of the decimal number.
            -Multiply the digit by i and add it to binaryNumber.
            -Increment i by a factor of 10.
            -Divide the decimal number by 2.
    Output: The variable binaryNumber contains the binary equivalent.

These algorithms use the principles of place value in the respective number systems to perform the conversions. The key idea is to convert from the source base (binary or octal) to decimal first and then from decimal to the target base (octal or binary).

 

Code Examples

#1 Code Example- C Programing to Convert Binary to Octal

Code - C Programming

#include <math.h>
#include <stdio.h>
int convert(long long bin);
int main() {
    long long bin;
    printf("Enter a binary number: ");
    scanf("%lld", &bin);
    printf("%lld in binary = %d in octal", bin, convert(bin));
    return 0;
}

int convert(long long bin) {
    int oct = 0, dec = 0, i = 0;

    // converting binary to decimal
    while (bin != 0) {
        dec += (bin % 10) * pow(2, i);
        ++i;
        bin /= 10;
    }
    i = 1;

    // converting to decimal to octal
    while (dec != 0) {
        oct += (dec % 8) * i;
        dec /= 8;
        i *= 10;
    }
    return oct;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter a binary number: 101001
101001 in binary = 51 in octal

#2 Code Example-C Programing to Convert Octal to Binary

Code - C Programming

#include <math.h>
#include <stdio.h>
long long convert(int oct);
int main() {
    int oct;
    printf("Enter an octal number: ");
    scanf("%d", &oct);
    printf("%d in octal = %lld in binary", oct, convert(oct));
    return 0;
}

long long convert(int oct) {
    int dec = 0, i = 0;
    long long bin = 0;

    // converting octal to decimal
    while (oct != 0) {
        dec += (oct % 10) * pow(8, i);
        ++i;
        oct /= 10;
    }
    i = 1;

   // converting decimal to binary
    while (dec != 0) {
        bin += (dec % 2) * i;
        dec /= 2;
        i *= 10;
    }
    return bin;
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
Enter an octal number: 67
67 in octal = 110111 in binary
Advertisements

Demonstration


C Programing Example to Convert Binary Number to Octal and vice-versa-DevsEnv

Next
Appending into a File in C Programming