In this C Programming example, we will discuss the conversion of decimal to octal and also implement the program to convert a decimal number to an octal representation.
Table of Contents
1. How to convert decimal to octal?
The decimal number is the most common number system which accepts the base value as 10. There are various direct or indirect methods to convert a decimal number to an octal number.
One such indirect approach is to convert a decimal number into another number system (e.g. hexadecimal).
Later, convert each digit of that number into a binary number.
Now, practicing grouping we can form an octal number system.
However, there are two direct ways used to convert a decimal number into an octal number −
Converting with Remainders
Converting with Division
1.1. Converting with Remainder
Let the decimal fractional part be M. Then multiply this number by 8.
Note down the value of the integer part, which will be 0, 1, 2, 3, 4, 5, 6, and 7.
Multiply the remaining decimal fractional number till it becomes 0.
Record the result of every integer part at each step.
Print the array (which will be an equivalent fractional octal number of given decimal fractional number).
Example: Convert decimal fractional number 0.140869140625 into octal number.
Since given number is decimal fractional number, so by using above algorithm performing short multiplication
by 8 with integer part.
Resultant integer part
0.140869140625 x 8 = 1.12695313
0.12695313 x 8 = 1.01562504
0.01562504 x 8 = 0.12500032
0.12500032 x 8 = 1.00000256
0.00000256 x 8 = 0.000020544
and so on.
Now, write these resultant integer part, this will be approximate 0.11010 which is equivalent octal
fractional number of decimal fractional 0.140869140625.
1.2. Converting with Division
In this method, we will divide the integer by 8 and take note of the reminder. Now take the result from the previous division and divide it again by 8 and continue the process until the integer is reduced to 0. The remainders that we have persisted in an array represent the octal number in reverse order.
Let’s have a look at the process step by step.
Begin with any decimal number.
Divide this number by 8 because 8 is the base of the octal numbers.
Note the remainder and store it in an array. The remainder should be from 0 to 7 as the divisor is 8.
Repeat steps 2 and 3 until the dividend is reduced to 0.
410 / 8 = 51
51 / 8 = 6
6 / 8 = 0
Final Octal number
Decimal to octal conversion by division
Helpful topics to understand this program better are-