VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-octal-decimal-conversion/

⇱ Program for Octal to Decimal Conversion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for Octal to Decimal Conversion

Last Updated : 15 Sep, 2023

Given an octal number as input, we need to write a program to convert the given octal number into equivalent decimal number.

Examples:

Input : 67
Output: 55
Input : 512
Output: 330
Input : 123
Output: 83

The idea is to extract the digits of a given octal number starting from the rightmost digit and keep a variable dec_value. At the time of extracting digits from the octal number, multiply the digit with the proper base (Power of 8) and add it to the variable dec_value. In the end, the variable dec_value will store the required decimal number.

For Example: 

If the octal number is 67. 

dec_value = 6*(8^1) + 7*(8^0) = 55

The below diagram explains how to convert an octal number (123) to an equivalent decimal value:  

👁 Image

Below is the implementation of the above idea. 


Output
55






Time complexity: O(logN) where N is the given number
Auxiliary space: O(1)

Method: Using look up table method

The function octalToDecimal takes an integer n as input, which represents the octal number that needs to be converted to decimal. It initializes an unordered map lookup that maps each octal digit to its decimal equivalent.

It then uses a loop to extract each digit of the octal number from right to left, starting from the least significant digit. For each digit, it multiplies the decimal equivalent of the digit (retrieved from the lookup table) with the appropriate power of 8 (base) and adds it to the decimal variable. The base variable is updated after each iteration by multiplying it with 8. The loop continues until all digits have been processed.

Finally, the decimal variable is returned as the output of the function.

In the main function, an octal number octal_num is initialized and passed as an argument to the octalToDecimal function. The resulting decimal value is printed to the console.


Output
55







Time complexity: The time complexity of this algorithm is O(log N), where N is the octal number being converted to decimal. This is because we loop through each digit in the octal number once, and the number of digits in an N-digit octal number is log N.

Auxiliary space: The space complexity of this algorithm is O(1), as we only store a fixed-size lookup table and a few integer variables for the running sum and base value.

Using predefined function


Output
55






Method 3: Using recursion

1. The method uses the fact that each digit of an octal number represents a power of 8, starting from the rightmost digit. 

2. The method extracts the rightmost digit of the octal number by taking the remainder of the number divided by 10 (i.e., octal % 10) and adds it to the product of the remaining digits and the appropriate power of 8 (i.e., 8 * octal_to_decimal(octal // 10)).

3.This recursive step continues until the entire number has been converted to decimal.


Output
55






The time complexity of this method is O(log n), where n is the given number

Auxiliary space: O(log n), where n is the given number

Comment
Article Tags:
Article Tags: