# Binary to Octal in C

In this article, we will discuss how to convert binary to octal in C.

## Introduction to Binary and Octal Number System:

Binary and octal are two different number systems used in computing. A number system is a set of symbols and rules for representing and manipulating numbers.

There are two particular ways of addressing numbers: Binary and Octal. We are familiar with the decimal system, which utilizes ten digits (0-9) to address numbers in everyday life. Again Binary utilizes only two digits - 0 and 1 - to address numbers. Numbers are represented by eight digits (0–7) in octal.

Binary is often used in computing because computers use digital signals that can be either on (1) or off (0) to represent data. By using binary, we can represent any number or character using just these two states.

Octal is one more number system that is here and there utilized in processing. It is not quite as normal as binary, but rather it is actually utilized in certain specific circumstances. Octal is helpful because it is a shorter way of addressing binary numbers. Each octal digit addresses three paired digits, so we can convert a binary number completely to an octal by grouping the digits into three sets and replacing each with a single octal digit.

For example, the binary number 11011010 can be gathered into three digit sets like this: 110 110 10. After that, each set can be replaced with an octal digit: 6 6 2. So the octal representation of 11011010 is 662.

Converting numbers between binary and octal (and other number systems) can help make them easier to handle and less difficult to understand. When converting binary to octal, grouping binary digits into threes and converting each group to an octal digit might make the number simpler to see and work with.

### Conversion Process from Binary to Octal:

The two basic phases in the conversion from binary to octal are grouping the binary digits into three digit groups and then converting each group into an octal digit.

The binary number 1101110101 can be converted to octal using the following example:

Step 1: Organize the binary digits into three digit groups starting with the digit on the right:

Code

11 0 11 1 01 0 1

|___| |___| |___| |___|

Note: To make the number of digits a different of three we should add a main zero on the grounds that the paired number contains 10 digits.

Step 2: Using the conversion table change each set of three binary digits to a single octal digit:

Binary Octal

000 0

001 1

010 2

011 3

100 4

101 5

110 6

111 7

When we apply the table to each group of three binary digits we obtain:

Code

110 111 010 1

|___| |___| |___| |___|

6 7 2 1

Step 3: Concatenate the octal digits to obtain the binary number is the final octal representation:

Code

1101110101 (binary) = 6721 (octal)

Any binary number can be converted to an octal using the same method.

### Understanding the Algorithm for Binary to Octal Conversion:

The calculation for changing a binary number over completely to an octal number is more basic and can be separated into the following steps:

1. Start with the binary number you wish to convert entirely to octal.

2. If the binary number has more than three digits add 0s to the left of the number until it is a multiple of three.

3. Starting with the rightmost digit divide the binary number into groups of three digits.

4. Using the conversion table below convert each set of three binary digits to a single octal digit:

Binary Octal

000 0

001 1

010 2

011 3

100 4

101 5

110 6

111 7

Concatenate the octal digits obtained in step 4 to obtain the final octal number.

Let us take an example to illustrate the algorithm:

Assume we need to change over the parallel number 110101011010 to octal.

Step 1: Since the binary number has 12 digits which is certainly not a multiple of 3, we add a 0 to one side to make it 001101010110.

Step 2: Separation of the binary number into group of 3 digits beginning from the furthest right digit: 001 101 010 110.

Step 3: Convert each group of 3 binary digits to a single octal digit using the conversion chart:

001 -> 1

101 -> 5

010 -> 2

110 -> 6

Step 4: Concatenate the octal digits obtained in step 3 to obtain the final octal number: 1526.

### Implementing Binary to Octal Conversion in C:

Here is some example code that executes binary to octal conversion in C:

Code

#include<stdio.h>
int main() {
int binary, octal = 0, i = 1;
printf("Enter a binary number: ");
scanf("%d", &binary);
while (binary != 0) {
int digit = binary % 10;
octal += digit * i;
i *= 2;
binary /= 10;
}
printf("Octal equivalent is: %d", octal);
return 0;
}

Output:

Enter a binary number: 101011
Octal equivalent is: 43