Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent octal number. i.e convert the number with base value 10 to base value 8. The base value of a number system determines the number of digits used to represent a numeric value. For example, the binary number system uses two digits 0 and 1, the octal number system uses 8 digits from 0-7 and the decimal number system uses 10 digits 0-9 to represent any numeric value.
Input : 16 Output : 20 Input : 10 Output : 12 Input: 33 Output: 41
- Store the remainder when the number is divided by 8 in an array.
- Divide the number by 8 now
- Repeat the above two steps until the number is not equal to 0.
- Print the array in reverse order now.
If the given decimal number is 16.
Step 1: Remainder when 16 is divided by 8 is 0. Therefore, arr = 0.
Step 2: Divide 16 by 8. New number is 16/8 = 2.
Step 3: Remainder, when 2 is divided by 8, is 2. Therefore, arr = 2.
Step 4: Divide 2 by 8. New number is 2/8 = 0.
Step 5: Since number becomes = 0. Stop repeating steps and print the array in reverse order. Therefore, the equivalent octal number is 20.
The below diagram shows an example of converting the decimal number 33 to an equivalent octal number.
Below is the implementation of the above idea.
Time Complexity: O(log N)
Thanks, harsh Agarwal for this approach.
Another Approach: (O(1) space Complexity)
This problem can also be solved without using an array using the following algorithm,:
- Initialize ocatalNum to 0 and countVal to 1 and the decimal number as n
- Find the remainder when decimal number divided by 8
- Update octal number by octalNum + (remainder * countval)
- Increase countval by countval*10
- Divide decimal number by 8
- Repeat from the second step until the decimal number is zero
Below is the implementation of the above idea:
Time Complexity: O(log N)
Auxiliary Space: O(1)
This article is contributed by Sumitav chakraborty . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Program for Octal to Decimal Conversion
- Decimal to octal conversion with minimum use of arithmetic operators
- 8086 program to convert a 16 bit Decimal number to Octal
- Check if Decimal representation of an Octal number is divisible by 7
- Program for Binary To Decimal Conversion
- Program for Decimal to Binary Conversion
- Decimal to binary conversion without using arithmetic operators
- Program to Convert Octal Number to Binary Number
- Program to Convert Hexadecimal to Octal
- Largest N digit Octal number which is a Perfect square
- Arithmetic Operations of Octal Numbers
- Convert a binary number to octal
- Octal equivalents of connected components in Binary valued graph
- Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation
- Program for Hexadecimal to Decimal
- Python program to convert decimal to binary number
- Recursive Program for Binary to Decimal
- 8086 program to Print a 16 bit Decimal number
- 8086 program to convert a 16 bit decimal number to binary
- 8086 program to convert a 16 bit decimal number to Hexadecimal