C program to convert binary to octal

 Here's the c program to convert binary to octal:

#include <stdio.h>

int binaryToOctal(int binaryNumber) {
    int octalNumber = 0, decimalNumber = 0, i = 0;

    // Convert binary to decimal
    while (binaryNumber != 0) {
        int remainder = binaryNumber % 10;
        decimalNumber += remainder * (1 << i);
        binaryNumber /= 10;
        i++;
    }

    // Convert decimal to octal
    i = 1;
    while (decimalNumber != 0) {
        int remainder = decimalNumber % 8;
        octalNumber += remainder * i;
        decimalNumber /= 8;
        i *= 10;
    }

    return octalNumber;
}

int main() {
    int binaryNumber;

    printf("Enter a binary number: ");
    scanf("%d", &binaryNumber);

    if (binaryNumber < 0) {
        printf("Please enter a positive binary number.\n");
    } else {
        int octalNumber = binaryToOctal(binaryNumber);
        printf("The octal equivalent is: %d\n", octalNumber);
    }

    return 0;
}

Explanation:

  1. We include the necessary standard input-output header file stdio.h.

  2. In the binaryToOctal function, we convert the binary number to an octal number in two steps: binary to decimal, and then decimal to octal.

  3. In the binaryToOctal function, we initialize octalNumber and decimalNumber to 0, and i to 0. octalNumber will store the octal result, and decimalNumber will store the intermediate decimal representation of the binary number. i is used as an exponent of 2 for binary to decimal conversion.

  4. We use a while loop to convert binary to decimal. We extract the least significant digit (LSB) of the binary number, multiply it by 2 to the power of i, and add it to decimalNumber. We then shift the binary number right by one digit by dividing it by 10 and increment i.

  5. After converting the binary number to decimal, we initialize i as 1 and use a while loop to convert decimal to octal. We extract the remainder when dividing the decimal number by 8, multiply it by i, and add it to octalNumber. We then divide the decimal number by 8 and multiply i by 10.

  6. In the main function, we take the user's input for the binary number.

  7. We check if the entered binary number is negative and prompt the user to enter a positive binary number if it's negative.

  8. If the binary number is non-negative, we call the binaryToOctal function to convert it to octal and then print the result.

    Example:

    Input:

    Enter a binary number: 110101

    Output:

    The octal equivalent is: 55

    In this example, the binary number 110101 is converted to its octal equivalent, which is 55.

Prasun Barua

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other published books are Solar PV System Design and Technology, Electricity from Renewable Energy, Tech Know Solar PV System, C Coding Practice, AI and Robotics Overview, Robotics and Artificial Intelligence, Know How Solar PV System, Know The Product, Solar PV Technology Overview, Home Appliances Overview, Tech Know Solar PV System, C Programming Practice, etc. These books are available at Google Books, Google Play, Amazon and other platforms.

*

Post a Comment (0)
Previous Post Next Post