C Program To Convert Octal Number To Binary Number, using While Loop

Lets write a C program to convert a number from Octal number system(base 8) to Binary number system(base 2), using while loop.

In this C program we first convert the user entered number from Octal number system to Decimal number system. Then we take the result(which is in Decimal number system) and convert to Binary number system.

Number Systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

Related Read:
while loop in C programming
C Program To Convert Octal Number To Decimal Number, using While Loop
C Program To Convert Decimal Number To Binary Number, using While Loop

Note:
In this C program we are dealing with Octal Number System, Decimal Number System and Binary Number System.

Octal Number System
Octal number system has base 8 and digits 0, 1, 2, 3, 4, 5, 6, 7.

Example:
etc .. 84, 83, 82, 81, 80

etc .. 84 = ‭4,096‬, 83 = 512, 82 = 64, 81 = 8, 80 = 1.

Binary Number System
Binary number system has base 2 and digits 0 and 1.

Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

Expected Output for the Input

User Input:
Enter an Octal Number
14

Output:
Binary Equivalent of Octal no 14 is 1100.

Video Tutorial: C Program To Convert Octal Number To Binary Number, using While Loop



YouTube Link: https://www.youtube.com/watch?v=_juS8cQRMYs [Watch the Video In Full Screen.]

Logic To Convert Octal Number To Binary Number, using While Loop

To get complete logic to this program kindly watch these two video tutorials without fail.

1. C Program To Convert Octal Number To Decimal Number, using While Loop
2. C Program To Convert Decimal Number To Binary Number, using While Loop

Source Code: C Program To Convert Octal Number To Binary Number, using While Loop

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

int main()
{
    int  num, dec = 0, rem = 0, place = 0;
    long bin = 0;

    printf("Enter an Octal Number\n");
    scanf("%d", &num);

    printf("Binary Equivalent of Octal no %d is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * pow(8, place);
        num = num / 10;
        place++;
    }

    place = 1;
    rem   = 0;
    while(dec)
    {
        rem   = dec % 2;
        bin   = bin + (rem * place);
        dec   = dec / 2;
        place = place * 10;
    }
    printf("%ld.\n", bin);

    return 0;
}

Output 1:
Enter an Octal Number
71
Binary Equivalent of Octal no 71 is 111001.

Output 2:
Enter an Octal Number
11
Binary Equivalent of Octal no 11 is 1001.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert