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:

Below is the implementation of the above idea.
C++
#include <iostream>
using namespace std;
int octalToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 8;
}
return dec_value;
}
int main()
{
int num = 67;
cout << octalToDecimal(num) << endl;
}
|
Java
import java.io.*;
class GFG {
static int octalToDecimal(int n)
{
int num = n;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 8;
}
return dec_value;
}
public static void main(String[] args)
{
int num = 67;
System.out.println(octalToDecimal(num));
}
}
|
Python3
def octalToDecimal(n):
num = n
dec_value = 0
base = 1
temp = num
while (temp):
last_digit = temp % 10
temp = int(temp / 10)
dec_value += last_digit * base
base = base * 8
return dec_value
num = 67
print(octalToDecimal(num))
|
C#
using System;
class GFG {
static int octalToDecimal(int n)
{
int num = n;
int dec_value = 0;
int b_ase = 1;
int temp = num;
while (temp > 0) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * b_ase;
b_ase = b_ase * 8;
}
return dec_value;
}
public static void Main()
{
int num = 67;
Console.WriteLine(octalToDecimal(num));
}
}
|
Javascript
<script>
function octalToDecimal(n)
{
let num = n;
let dec_value = 0;
let base = 1;
let temp = num;
while (temp) {
let last_digit = temp % 10;
temp = Math.floor(temp / 10);
dec_value += last_digit * base;
base = base * 8;
}
return dec_value;
}
let num = 67;
document.write(octalToDecimal(num) + "<br>");
</script>
|
PHP
<?php
function octalToDecimal($n)
{
$num = $n;
$dec_value = 0;
$base = 1;
$temp = $num;
while ($temp)
{
$last_digit = $temp % 10;
$temp = $temp / 10;
$dec_value += $last_digit * $base;
$base = $base * 8;
}
return $dec_value;
}
$num = 67;
echo octalToDecimal($num);
?>
|
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.
C++
#include <iostream>
#include <unordered_map>
using namespace std;
int octalToDecimal(int n) {
unordered_map<int, int> lookup{
{0, 0}, {1, 1}, {2, 2}, {3, 3},
{4, 4}, {5, 5}, {6, 6}, {7, 7}
};
int decimal = 0;
int base = 1;
while (n > 0) {
int last_digit = n % 10;
decimal += lookup[last_digit] * base;
n /= 10;
base *= 8;
}
return decimal;
}
int main() {
int octal_num = 67;
cout << octalToDecimal(octal_num) << endl;
return 0;
}
|
Java
import java.util.HashMap;
public class GFG {
public static int octalToDecimal(int n) {
HashMap<Integer, Integer> lookup = new HashMap<>();
lookup.put(0, 0);
lookup.put(1, 1);
lookup.put(2, 2);
lookup.put(3, 3);
lookup.put(4, 4);
lookup.put(5, 5);
lookup.put(6, 6);
lookup.put(7, 7);
int decimal = 0;
int base = 1;
while (n > 0) {
int lastDigit = n % 10;
decimal += lookup.get(lastDigit) * base;
n /= 10;
base *= 8;
}
return decimal;
}
public static void main(String[] args) {
int octalNum = 67;
System.out.println(octalToDecimal(octalNum));
}
}
|
Python3
def octal_to_decimal(n):
lookup = {
0: 0, 1: 1, 2: 2, 3: 3,
4: 4, 5: 5, 6: 6, 7: 7
}
decimal = 0
base = 1
while n > 0:
last_digit = n % 10
decimal += lookup[last_digit] * base
n //= 10
base *= 8
return decimal
def main():
octal_num = 67
print(octal_to_decimal(octal_num))
if __name__ == "__main__":
main()
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int OctalToDecimal(int n)
{
Dictionary<int, int> lookup = new Dictionary<int, int>()
{
{0, 0}, {1, 1}, {2, 2}, {3, 3},
{4, 4}, {5, 5}, {6, 6}, {7, 7}
};
int decimalNum = 0;
int baseVal = 1;
while (n > 0)
{
int lastDigit = n % 10;
decimalNum += lookup[lastDigit] * baseVal;
n /= 10;
baseVal *= 8;
}
return decimalNum;
}
public static void Main(string[] args)
{
int octalNum = 67;
int decimalValue = OctalToDecimal(octalNum);
Console.WriteLine("Equivalent Decimal Value = " + decimalValue);
}
}
|
Javascript
function octalToDecimal(n) {
let lookup = new Map([
[0, 0], [1, 1], [2, 2], [3, 3],
[4, 4], [5, 5], [6, 6], [7, 7]
]);
let decimal = 0;
let base = 1;
while (n > 0) {
let last_digit = n % 10;
decimal += lookup.get(last_digit) * base;
n = Math.floor(n/10);
base *= 8;
}
return decimal;
}
let octal_num = 67;
document.write(octalToDecimal(octal_num));
|
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
C++
#include <iostream>
using namespace std;
int OctToDec(string n)
{
return stoi(n, 0, 8);
}
int main()
{
string n = "67";
cout << OctToDec(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int OctToDec(String n)
{
return Integer.parseInt(n, 8);
}
public static void main(String[] args)
{
String n = "67";
System.out.println(OctToDec(n));
}
}
|
Python3
def OctToDec(n):
return int(n, 8);
if __name__ == '__main__':
n = "67";
print(OctToDec(n));
|
C#
using System;
public class GFG{
public static int OctToDec(String n)
{
return Convert.ToInt32(n, 8);
}
static public void Main (){
string n = "67";
Console.WriteLine(OctToDec(n));
}
}
|
Javascript
<script>
function OctToDec(n)
{
return parseInt(n, 8);
}
var n = "67";
document.write(OctToDec(n));
</script>
|
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.
C++
#include <iostream>
using namespace std;
int octal_to_decimal(int octal) {
if (octal == 0) {
return 0;
} else {
return (octal % 10) + 8 * octal_to_decimal(octal / 10);
}
}
int main() {
int octal = 67;
cout << octal_to_decimal(octal) << endl;
return 0;
}
|
Java
public class OctalToDecimal {
public static int octalToDecimal(int octal) {
if (octal == 0) {
return 0;
} else {
return (octal % 10) + 8 * octalToDecimal(octal / 10);
}
}
public static void main(String[] args) {
int octal = 67;
System.out.println(octalToDecimal(octal));
}
}
|
Python3
def octal_to_decimal(octal):
if octal == 0:
return 0
else:
return (octal % 10) + 8 * octal_to_decimal(octal // 10)
octal=67
print(octal_to_decimal(octal))
|
C#
using System;
public class Program
{
public static void Main()
{
int octal = 67;
int decimalNum = OctalToDecimal(octal);
Console.WriteLine(decimalNum);
}
public static int OctalToDecimal(int octal)
{
if (octal == 0)
{
return 0;
}
else
{
return (octal % 10) + 8 * OctalToDecimal(octal / 10);
}
}
}
|
Javascript
function octal_to_decimal(octal) {
if (octal == 0) {
return 0;
} else {
return (octal % 10) + 8 * octal_to_decimal(Math.floor(octal / 10));
}
}
let octal = 67;
console.log(octal_to_decimal(octal));
|
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
Similar Reads
Ripple Counter in Digital Logic
Counters play a crucial role in digital logic circuits, enabling tasks such as clock frequency division and sequencing. This article explores the concept of ripple counters, a type of asynchronous counter, their operation, advantages, and disadvantages in digital logic design. What is a Counter?Coun
5 min read
Design counter for given sequence
A Counter is a device which stores (and sometimes displays) the number of times a particular event or process has occurred, often in relationship to a clock signal. Counters are used in digital electronics for counting purpose, they can count specific event happening in the circuit. For example, in
3 min read
Master-Slave JK Flip Flop
Prerequisite -Flip-flop types and their ConversionRace Around Condition In JK Flip-flop - For J-K flip-flop, if J=K=1, and if clk=1 for a long period of time, then Q output will toggle as long as CLK is high, which makes the output of the flip-flop unstable or uncertain. This problem is called race
4 min read
Asynchronous Sequential Circuits
An asynchronous sequential circuit does not have the clock signal; the transitions between different states occur due to the âchange of inputsâ. This makes them suitable for applications which involve low power input or when a clock signal may not be needed. In this article, we explain how these cir
7 min read
Shift Registers in Digital Logic
Pre-Requisite: Flip-Flops Flip flops can be used to store a single bit of binary data (1 or 0). However, in order to store multiple bits of data, we need multiple flip-flops. N flip flops are to be connected in order to store n bits of data. A Register is a device that is used to store such informat
8 min read
Design 101 sequence detector (Mealy machine)
A sequence detector is a sequential state machine that takes an input string of bits and generates an output 1 whenever the target sequence has been detected. In a Mealy machine, output depends on the present state and the external input (x). Hence, in the diagram, the output is written outside the
5 min read
Amortized analysis for increment in counter
Amortized analysis refers to determining the time-averaged running time for a sequence (not an individual) operation. It is different from average case analysis because here, we don't assume that the data arranged in average (not very bad) fashion like we do for average case analysis for quick sort.
4 min read
Number System and Base Conversions
Electronic and Digital systems may use a variety of different number systems, (e.g. Decimal, Hexadecimal, Octal, Binary), or even Duodecimal or less well known but better named Uncial. All the other bases other than Decimal result from computer usage. Uncial (named from Latin for 1/12 "uncia" the ba
5 min read
Code Converters - BCD(8421) to/from Excess-3
Prerequisite - Number System and base conversions Excess-3 binary code is an unweighted self-complementary BCD code. Self-Complementary property means that the 1's complement of an excess-3 number is the excess-3 code of the 9's complement of the corresponding decimal number. This property is useful
5 min read
Code Converters - Binary to/from Gray Code
In this article, we will go through Code Converters - Binary to/from Gray Code,we will start our article by defining Code converters, Binary code and Gray code, and then we will go through the conversion of binary code to gray code and vice versa. At last, we will conclude our article with some faqs
9 min read