Program for Octal to Decimal Conversion

05 Apr, 2022
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. 
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
default, selected
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
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 above idea. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to convert octal to decimal#include <iostream>using namespace std; // Function to convert octal to decimalint octalToDecimal(int n){    int num = n;    int dec_value = 0;     // Initializing base value to 1, i.e 8^0    int base = 1;     int temp = num;    while (temp) {         // Extracting last digit        int last_digit = temp % 10;        temp = temp / 10;         // Multiplying last digit with appropriate        // base value and adding it to dec_value        dec_value += last_digit * base;         base = base * 8;    }     return dec_value;} // Driver program to test above functionint main(){    int num = 67;     cout << octalToDecimal(num) << endl;}
// Java program to convert octal to decimalimport java.io.*; class GFG {     // Function to convert octal to decimal    static int octalToDecimal(int n)    {        int num = n;        int dec_value = 0;         // Initializing base value to 1, i.e 8^0        int base = 1;         int temp = num;        while (temp > 0) {            // Extracting last digit            int last_digit = temp % 10;            temp = temp / 10;             // Multiplying last digit with appropriate            // base value and adding it to dec_value            dec_value += last_digit * base;             base = base * 8;        }        return dec_value;    }     // driver program    public static void main(String[] args)    {        int num = 67;        System.out.println(octalToDecimal(num));    }} // This code is contributed// by Pramod Kumar
# Python3 program to convert# octal to decimal # Function to convert# octal to decimal  def octalToDecimal(n):     num = n    dec_value = 0     # Initializing base value    # to 1, i.e 8^0    base = 1     temp = num    while (temp):         # Extracting last digit        last_digit = temp % 10        temp = int(temp / 10)         # Multiplying last digit        # with appropriate base        # value and adding it        # to dec_value        dec_value += last_digit * base         base = base * 8     return dec_value  # Driver Codenum = 67print(octalToDecimal(num)) # This code is contributed by mits
// C# program to convert octal to// decimalusing System; class GFG {     // Function to convert octal    // to decimal    static int octalToDecimal(int n)    {        int num = n;        int dec_value = 0;         // Initializing base value        // to 1, i.e 8^0        int b_ase = 1;         int temp = num;        while (temp > 0) {             // Extracting last digit            int last_digit = temp % 10;            temp = temp / 10;             // Multiplying last digit            // with appropriate base            // value and adding it to            // dec_value            dec_value += last_digit * b_ase;             b_ase = b_ase * 8;        }        return dec_value;    }     // driver program    public static void Main()    {        int num = 67;         Console.WriteLine(octalToDecimal(num));    }} // This code is contributed by vt_m.
<?php// PHP program to convert octal to decimal // Function to convert// octal to decimalfunction octalToDecimal($n){         $num = $n;    $dec_value = 0;     // Initializing base value    // to 1, i.e 8^0    $base = 1;     $temp = $num;    while ($temp)    {         // Extracting last digit        $last_digit = $temp % 10;        $temp = $temp / 10;         // Multiplying last digit        // with appropriate base        // value and adding it        // to dec_value        $dec_value += $last_digit * $base;         $base = $base * 8;    }     return $dec_value;}     // Driver Code    $num = 67;    echo octalToDecimal($num);     // This code is contributed by anuj_67?>
<script> // JavaScript program to convert octal to decimal // Function to convert octal to decimalfunction octalToDecimal(n){    let num = n;    let dec_value = 0;     // Initializing base value to 1, i.e 8^0    let base = 1;     let temp = num;    while (temp) {         // Extracting last digit        let last_digit = temp % 10;        temp = Math.floor(temp / 10);         // Multiplying last digit with appropriate        // base value and adding it to dec_value        dec_value += last_digit * base;         base = base * 8;    }     return dec_value;} // Driver program to test above function     let num = 67;     document.write(octalToDecimal(num) + "<br>");  // This code is contributed by Surbhi Tyagi </script>
55
Using predefined function
C++
Java
Python3
C#
Javascript
// C++ program to convert octal to decimal#include <iostream>using namespace std;int OctToDec(string n){  return stoi(n, 0, 8);}int main(){   string n = "67";  cout << OctToDec(n);  return 0;} // This code is contributed by phasing17
// Java program to convert octal to decimalimport 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));    }}
# Python program to convert octal to decimaldef OctToDec(n):    return int(n, 8); if __name__ == '__main__':     n = "67";    print(OctToDec(n));     # This code is contributed by 29AjayKumar
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));    }} // THIS CODE IS CONTRIBUTED BY RAG2127
<script>// javascript program to convert octal to decimal   function OctToDec(n)    {        return parseInt(n, 8);    }        var n = "67";   document.write(OctToDec(n));// This code contributed by Princi Singh</script>
55
 
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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. 
vt_m
Mithun Kumar
surbhityagi15
le0
princi singh
29AjayKumar
rag2127
phasing17
base-conversion
Computer Organization & Architecture
Digital Electronics & Logic Design
GATE CS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cache Memory in Computer Organization
Addressing Modes
Logical and Physical Address in Operating System
Direct Access Media (DMA) Controller in Computer Architecture
Computer Organization | RISC and CISC
IEEE Standard 754 Floating Point Numbers
Difference between RAM and ROM
Introduction to memory and memory units
Analog to Digital Conversion
Difference between Half adder and full adder