Program to Convert Binary Number to Octal and vice-versa - (C Programs)
Introduction
In this program, we will convert a given binary number to octal and vice-versa. Binary number system is the base-2 numbering system while octal number system is the base-8 numbering system.
Example
Example 1: Binary to Octal
Enter binary number: 101010
Octal conversion of 101010 is 52
Example 2: Octal to Binary
Enter octal number: 127
Binary conversion of 127 is 1011111
Output
- If converting from binary to octal, program will display the octal equivalent of the binary input.
- If converting from octal to binary, program will display the binary equivalent of the octal input.
- If input is invalid, program will display an error message.
Explanation
Binary to octal conversion
Split the binary number into groups of three digits, starting from right to left. If the leftmost group has less than three digits, add zeroes to the left to make it a group of three.
Convert each group of three digits to its octal equivalent as shown below:
Binary | Octal |
---|---|
000 | 0 |
001 | 1 |
010 | 2 |
011 | 3 |
100 | 4 |
101 | 5 |
110 | 6 |
111 | 7 |
- Write the octal numbers obtained from each group together to get the octal equivalent of the binary input.
Octal to binary conversion
- Convert each octal digit to its binary equivalent as shown below:
Octal | Binary |
---|---|
0 | 000 |
1 | 001 |
2 | 010 |
3 | 011 |
4 | 100 |
5 | 101 |
6 | 110 |
7 | 111 |
- Write the binary numbers obtained from each octal digit together to get the binary equivalent of the octal input.
Use
This program can be used to easily convert binary numbers to octal and vice-versa.
Summary
This program allows easy conversion between binary and octal numbers. The user can input either a binary or octal number and the program will output the equivalent octal or binary number respectively.