In this tutorial, we will write a C program to convert an octal number into binary using a while loop. Before that, you must have knowledge of the following topics in C.
Binary number
The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.
Octal number
The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.
Example:
Input: 51
Output: 101001
Let us go through a program for the Octal to Binary Conversion in C.
C Program to Convert Octal to Binary
Source code: convert octal number to binary number using function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <math.h> #include <stdio.h> int conversionFunc(int octalNum) { int decNum = 0, i = 0; int binaryNum = 0; while (octalNum != 0) { decNum += (octalNum % 10) *pow(8, i); ++i; octalNum /= 10; } i = 1; while (decNum != 0) { binaryNum += (decNum % 2) *i; decNum = decNum / 2; i *= 10; } return binaryNum; } int main() { int octalNum; printf("Enter an octal number: "); scanf("%d", &octalNum); printf("Equivalent Binary Value: %d", conversionFunc(octalNum)); return 0; } |
Output:
Enter an octal number: 51
Octal Binary Value: 101001