C Program to convert a number from decimal to octal
|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.
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.
Multiplication | Resultant integer part |
0.140869140625 x 8 = 1.12695313 | 1 |
0.12695313 x 8 = 1.01562504 | 1 |
0.01562504 x 8 = 0.12500032 | 0 |
0.12500032 x 8 = 1.00000256 | 1 |
0.00000256 x 8 = 0.000020544 | 0 |
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.
Division | Remainder (R) |
---|---|
410 / 8 = 51 | 2 |
51 / 8 = 6 | 3 |
6 / 8 = 0 | 6 |
Final Octal number | 632 |
Helpful topics to understand this program better are-
2. C Program to convert a number from decimal to octal
Let’s examine the execution(kind of pseudocode) of the program to convert a number from decimal to octal in C.
- Initially, the program will prompt the user to enter a decimal number and store the input value in
int decimalNum
. - Now, the
convertDecimalToOctal(int decimalNum)
function is invoked. - In the function, calculations are performed on the input using a while loop and the concept of Converting with Division.
- Later, the output is obtained and it gets printed on the console.
Let us now implement the above execution of the program to convert a number from decimal to octal in C.
#include <math.h> #include <stdio.h> int convertDecimalToOctal(int decimalNum); int convertDecimalToOctal(int decimalNum) { int octalNum = 0, i = 1; while (decimalNum != 0) { octalNum += (decimalNum % 8) * i; decimalNum /= 8; i *= 10; } return octalNum; } int main() { int decimalNum; printf("Enter any decimal number: "); scanf("%d", &decimalNum); printf("%d in decimal is %d in octal", decimalNum, convertDecimalToOctal(decimalNum)); return 0; }
Note: In the above program we have used int data type to store the input. However, we can use float, double, long datatype with their format specifier as well.
Output Enter any decimal number: 130 130 in decimal = 202 in octal
3. Conclusion
In this C Programming example, we have discussed how to convert a number from decimal to octal for fraction and non fraction part and also implemented the program for the same.
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!! ?