Q:

C program to convert an octal number into a binary number

0

C program to convert an octal number into a binary number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here, we will read an octal number and convert it into a binary number and print the result on the console screen.

Program:

The source code to convert an octal number into a binary number is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to convert an octal number 
// into binary number

#include <stdio.h>

int main()
{
    char octalNum[32];
    int i = 0;

    printf("Enter octal number: ");
    scanf("%s", octalNum);

    printf("Binay number: ");
    while (octalNum[i]) {
        switch (octalNum[i]) {
        case '0':
            printf("000");
            break;

        case '1':
            printf("001");
            break;

        case '2':
            printf("010");
            break;

        case '3':
            printf("011");
            break;

        case '4':
            printf("100");
            break;

        case '5':
            printf("101");
            break;

        case '6':
            printf("110");
            break;

        case '7':
            printf("111");
            break;

        default:
            printf("\nInvalid octal number");
            return 0;
        }
        i++;
    }

    return 0;
}

Output:

Enter octal number: 123
Binay number: 001010011

Explanation:

Here, we created a character array octalNum and integer variable i. Then we read the octal number and convert the number into a binary number and printed the result on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< C program to convert a binary number to a hexadeci...