A number system with a base 2 is known as binary number system. Hence, binary numbers are denoted with a base 2. It consists of two values: 0 and 1. Each digit in this system is said to be a bit.
For example: (001101)2, (10001101)2, (11100)2, (10)2, etc.
A number system with a base 8 is known as octal number system. Hence, octal numbers are denoted with a base 8. It consists of the values: 0,1,2,3,4,5,6,7.
For example: (145)8, (36)8, (120)8, (4105)8, etc.
There are many ways to convert a binary number to its octal equivalent. Here, to perform the conversion, we will first convert the binary number into decimal number system, and then change that decimal number to its octal equivalent.
We begin by taking the binary number as input from the user. We will first convert this number into decimal. To do so, we first initialize the decimal number, say ‘d’ as zero, and the octal number, say ‘octal’ as zero. To convert the binary number to decimal, we extract the last digit from the binary number one at a time and multiply it with 2 to the power of the number’s position. We repeat this process for all the digits of the number and then this product is added to get the decimal number.
Once we have the decimal number, we convert this into octal number system. To do this conversion, we divide the decimal number by 8 until zero is obtained. The remainder becomes the most significant bit of the octal number in each division.
To perform this conversion, we start a loop that runs until ‘d’ is equal to zero. We extract that remainder with the help of the modulus operator. After that, the remainder is multiplied by the place (starting from ones) in the octal number. We use a variable ‘i’ to keep track of the place where the next remainder is to be inserted. This variable is incremented after each loop iteration. Once the loop stops execution, we display the octal number.