📌  相关文章
📜  八进制转换为十进制的程序

📅  最后修改于: 2021-06-28 16:10:12             🧑  作者: Mango

给定一个八进制数作为输入,我们需要编写一个程序将给定的八进制数转换为等效的十进制数。

例子:

Input : 67
Output: 55

Input : 512
Output: 330

Input : 123
Output: 83

这个想法是从最右边的数字开始提取给定八进制数字的数字,并保留变量dec_value。从八进制数字中提取数字时,将数字乘以适当的基数(8的幂),然后将其添加到变量dec_value中。最后,变量dec_value将存储所需的十进制数字。

例如:

如果八进制数是67。

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

下图说明了如何将八进制数(123)转换为等效的十进制值:

以下是上述想法的实现。

C++
// C++ program to convert octal to decimal
#include 
using namespace std;
 
// Function to convert octal to decimal
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) {
 
        // 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 function
int main()
{
    int num = 67;
 
    cout << octalToDecimal(num) << endl;
}


Java
// Java program to convert octal to decimal
import 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
# 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 Code
num = 67
print(octalToDecimal(num))
 
# This code is contributed by mits


C#
// C# program to convert octal to
// decimal
using 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


Javascript


Java
// Java program to convert octal to decimal
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
# Python program to convert octal to decimal
def OctToDec(n):
    return int(n, 8);
 
if __name__ == '__main__':
 
    n = "67";
    print(OctToDec(n));
     
# This code is contributed by 29AjayKumar


Javascript


输出
55

使用预定义函数

Java

// Java program to convert octal to decimal
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

# Python program to convert octal to decimal
def OctToDec(n):
    return int(n, 8);
 
if __name__ == '__main__':
 
    n = "67";
    print(OctToDec(n));
     
# This code is contributed by 29AjayKumar

Java脚本


输出
55