Decimal to octal conversion
In this section, we can convert any decimal number(base 10) into octal number(base 8) by c program and with example.
Example: Program to convert Decimal to Octal
#include <stdio.h>
// function to calculate the octal value of the given
void decimaltoOctal(int deciNum)
{
int octalNum = 0, countval = 1;
int dNo = deciNum;
while (deciNum != 0) {
// decimals remainder is calculated
int remainder = deciNum % 8;
// storing the octalvalue
octalNum += remainder * countval;
// storing exponential value
countval = countval * 10;
deciNum /= 8;
}
printf("%d", octalNum);
}
// Driver Code
int main()
{
int n = 33;
// Function Call
decimaltoOctal(n);
return 0;
}
Output : 41
PRACTICE PROBLEMS BASED ON DECIMAL TO OCTAL CONVERSION-
Convert the following numbers from base 10 to base 8-
- (1032)10
- (1032.6875)10
Solution-
1. (1032)10
(1032)10 → (?)8
Using division method, we have-
From here, (1032)10 = (2010)8
2. (1032.6875)10
(1032.6875)10 → ( ? )8
Here, we treat the real part and fractional part separately-
For Real Part-
- The real part is (1032)10
- We convert the real part from base 10 to base 8 using division method same as above.
So, (1032)10 = (2010)8
For Fractional Part-
- The fractional part is (0.6875)10
- We convert the fractional part from base 10 to base 8 using multiplication method.
Using multiplication method, we have-
Explanation
Step-01:
Step-02:
Since fractional part becomes 0, so we stop. |
- The fractional part terminates to 0 after 2 iterations.
- Traverse the real part column from top to bottom to obtain the required number in base 8.
From here, (0.6875)10 = (0.54)8
Combining the result of real and fractional parts, we have-
(1032.6875)10 = (2010.54)8