C Program to Convert Binary to Octal
|In this C Programming example, we will implement the program to convert a number from binary to octal and print the output on the console.
1. How to convert binary to octal?
Binary is the simplest kind of number system that accepts only two digits of 0 and 1 (i.e. the value of base 2).
The octal numeral system is the base-8 number system and accepts the digits from 0 to 7. Octal numerals can be formed using binary numerals and grouping the consecutive binary digits into groups of three (starting from the right).
This is a simple algorithm where you have to group binary number and replace their equivalent octal digit.
- Take a binary number.
- Split the binary digits into combinations of three digits from the right and from the left for the decimal part.
- Convert each combination of three binary digits into one octal number.
Example Input: 1010101 CONVERSION: (1010101)2 to Octal Splitting into group of 3 digits from the right 1 010 101 Converting each group to its octal/decimal representation 1 010 101 = (1 2 5)8 = (125)8 Output: (1010101)2 = (125)8
Helpful topics to understand this program better are-
- Data Types in C
- Console Input / Output in C
- Functions in C
- Recursive functions in C
- For and While loop in C
- C program to convert decimal to octal
2. C Program to convert a number from binary to octal
Let’s examine the execution(kind of pseudocode) of the program to convert a number from binary to octal in C.
- Initially, the program will prompt the user to enter a binary number and store the input in a
long binary
. - A function
convertBinarytoOctal(binary)
is invoked and using a while loop the calculations are performed on the input using the concepts we discussed above. - The obtained result is then printed on the console.
Let us now implement the above execution of the program to convert a number from binary to octal in C.
#include <math.h> #include <stdio.h> int binaryToDecimal(long num) { int decimal = 0, i = 0, rem; while (num != 0) { rem = num % 10; num /= 10; decimal += rem * pow(2, i++); } return decimal; } int convertBinarytoOctal(long binary) { int octal = 0, decimal = 0, i = 0; while (binary != 0) { int temp = binary % 1000; octal += binaryToDecimal(temp) * pow(10, i++); binary = binary / 1000; } return octal; } int main() { long binary; printf("Enter a binary number: "); scanf("%ld", &binary); printf("%ld in binary is %d in octal", binary, convertBinarytoOctal(binary)); return 0; }
Note: In the above program we have used a long data type to store the input. However, we can use float, double, int datatype with their format specifier as well.
Output Enter a binary number: 1010 1010 in binary is 12 in octal
There can be a slight variation for this program which is a bit easier to understand. The variation includes conversion of Binary to Decimal and then convert the Decimal to Octal. Using this method we do not need to split the number into a group of three digits.
Let’s implement the example by replacing methods convertBinarytoOctal()
and binaryToDecimal()
by the implementation below.
int convertBinarytoOctal(long binary) { int octal = 0, decimal = 0, i = 0; while (binary != 0) { decimal += (binary % 10) * pow(2, i++); binary /= 10; } i = 0; while (decimal != 0) { octal += (decimal % 8) * pow(10, i++); decimal /= 8; } return octal; }
3. Conclusion
In this C Programming example, we have discussed how to convert a number from binary to octal in C.
Helpful Links
Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.
Also for the example C programs please refer to C Programming Examples.
All examples are hosted on Github.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?