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

📅  最后修改于: 2021-09-27 15:18:53             🧑  作者: Mango

给定一个十进制数作为输入,我们需要编写一个程序将给定的十进制数转换为等效的八进制数。即,将基值 10 的数字转换为基值 8。数字系统的基值决定了用于表示数值的位数。例如,二进制数字系统使用 0 和 1 两个数字,八进制数字系统使用 0-7 的 8 位数字系统,十进制数字系统使用 0-9 10 位数字表示任何数值。

例子:

Input : 16
Output: 20

Input : 10
Output: 12

Input : 33
Output: 41

算法

  1. 将数字除以 8 时的余数存储在数组中。
  2. 现在将数字除以 8
  3. 重复以上两步,直到数字不等于0。
  4. 现在以相反的顺序打印数组。

例如:

如果给定的十进制数是 16。

步骤一:16除以8的余数为0,因此arr[0] = 0。
第 2 步:将 16 除以 8。新数是 16/8 = 2。
第 3 步:余数,当 2 除以 8 时,为 2。因此,arr[1] = 2。
第 4 步:将 2 除以 8。新数为 2/8 = 0。
第 5 步:由于数字变为 = 0。

停止重复步骤并以相反的顺序打印数组。因此,等效的八进制数是 20。

下图显示了将十进制数 33 转换为等效的八进制数的示例。

十进制

下面是上述想法的实现。

C++
// C++ program to convert a decimal
// number to octal number
 
#include 
using namespace std;
 
// function to convert decimal to octal
void decToOctal(int n)
{
 
    // array to store octal number
    int octalNum[100];
 
    // counter for octal number array
    int i = 0;
    while (n != 0) {
 
        // storing remainder in octal array
        octalNum[i] = n % 8;
        n = n / 8;
        i++;
    }
 
    // printing octal number array in reverse order
    for (int j = i - 1; j >= 0; j--)
        cout << octalNum[j];
}
 
// Driver Code
int main()
{
    int n = 33;
 
    // Function Call
    decToOctal(n);
 
    return 0;
}


Java
// Java program to convert a decimal
// number to octal number
import java.io.*;
 
class GFG {
    // Function to convert decimal to octal
    static void decToOctal(int n)
    {
        // array to store octal number
        int[] octalNum = new int[100];
 
        // counter for octal number array
        int i = 0;
        while (n != 0) {
            // storing remainder in octal array
            octalNum[i] = n % 8;
            n = n / 8;
            i++;
        }
 
        // Printing octal number array in reverse order
        for (int j = i - 1; j >= 0; j--)
            System.out.print(octalNum[j]);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 33;
 
        // Function Call
        decToOctal(n);
    }
}
 
// Contributed by Pramod Kumar


Python3
# Python3 program to convert
# a decimal number to
# octal number
 
# function to convert
# decimal to octal
 
 
def decToOctal(n):
 
    # array to store
    # octal number
    octalNum = [0] * 100
 
    # counter for octal
    # number array
    i = 0
    while (n != 0):
 
        # storing remainder
        # in octal array
        octalNum[i] = n % 8
        n = int(n / 8)
        i += 1
 
    # printing octal number
    # array in reverse order
    for j in range(i - 1, -1, -1):
        print(octalNum[j], end="")
 
 
# Driver Code
n = 33
 
# Function Call
decToOctal(n)
 
# This code is contributed
# by mits


C#
// C# program to convert a decimal
// number to octal number
using System;
 
class GFG {
 
    // Function to convert decimal to octal
    static void decToOctal(int n)
    {
 
        // array to store octal number
        int[] octalNum = new int[100];
 
        // counter for octal number array
        int i = 0;
        while (n != 0) {
 
            // storing remainder in octal array
            octalNum[i] = n % 8;
            n = n / 8;
            i++;
        }
 
        // Printing octal number array in
        // reverse order
        for (int j = i - 1; j >= 0; j--)
            Console.Write(octalNum[j]);
    }
 
    // Driver Code
    public static void Main()
    {
        int n = 33;
 
        // Function Call
        decToOctal(n);
    }
}
 
// This code is contributed by nitin mittal.


PHP
= 0; $j--)
        echo $octalNum[$j];
}
 
// Driver Code
$n = 33;
 
// Function Call
decToOctal($n);
 
// This code is contributed
// by ajit
?>


Javascript


C++
// C++ program to convert decimal
// number to octal number
#include 
using namespace std;
 
// function to calculate the octal value of the given
// decimal number
void decimaltoOctal(int deciNum)
{
 
    // initializations
    int octalNum = 0, countval = 1;
    int dNo = deciNum;
 
    while (deciNum != 0) {
 
        // decimals remainder is calculated
        int remainder = deciNum % 8;
 
        // storing the octalvalue
        octalNum += remainder * countval;
 
        // storing exponential value
        countval = countval * 10;
        deciNum /= 8;
    }
    cout << octalNum << endl;
}
 
// Driver Code
int main()
{
    int n = 33;
 
    // Function Call
    decimaltoOctal(n);
    return 0;
}


C
// C program to convert decimal
// number to octal number
#include 
 
// function to calculate the octal value of the given
// decimal number
void decimaltoOctal(int deciNum)
{
 
    int octalNum = 0, countval = 1;
    int dNo = deciNum;
 
    while (deciNum != 0) {
        // decimals remainder is calculated
        int remainder = deciNum % 8;
 
        // storing the octalvalue
        octalNum += remainder * countval;
 
        // storing exponential value
        countval = countval * 10;
        deciNum /= 8;
    }
    printf("%d", octalNum);
}
 
// Driver Code
int main()
{
    int n = 33;
 
    // Function Call
    decimaltoOctal(n);
    return 0;
}


Java
// JAVA program to convert decimal
// number to octal number
 
import java.io.*;
 
class GFG {
    // function to calculate the octal value of the given
    // decimal number
    static void octaltodecimal(int deciNum)
    {
        int octalNum = 0, countval = 1;
        int dNo = deciNum;
        while (deciNum != 0) {
 
            // decimals remainder is calculated
            int remainder = deciNum % 8;
 
            // storing the octalvalue
            octalNum += remainder * countval;
 
            // storing exponential value
            countval = countval * 10;
            deciNum /= 8;
        }
        System.out.println(octalNum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 33;
 
        // Function Call
        octaltodecimal(n);
    }
}


Python3
# Python3 program to convert decimal
# number to octal number
 
# function to calculate the octal value of the given
# decimal number
 
 
def decimaltoOctal(deciNum):
 
    # initializations
    octalNum = 0
    countval = 1
    dNo = deciNum
 
    while (deciNum != 0):
 
        # decimals remainder is calculated
        remainder = deciNum % 8
 
        # storing the octalvalue
        octalNum += remainder * countval
 
        # storing exponential value
        countval = countval * 10
        deciNum //= 8
 
    print(octalNum)
 
 
# Driver Code
if __name__ == '__main__':
 
    n = 33
 
    # Function Call
    decimaltoOctal(n)
 
# This code is contributed by pratham76


C#
// C# program to convert decimal
// number to octal number
using System;
class GFG {
 
    // function to calculate
    // the octal value of the given
    // decimal number
    static void octaltodecimal(int deciNum)
    {
        int octalNum = 0, countval = 1;
 
        while (deciNum != 0) {
            // decimals remainder is
            // calculated
            int remainder = deciNum % 8;
 
            // storing the octalvalue
            octalNum += remainder * countval;
 
            // storing exponential value
            countval = countval * 10;
            deciNum /= 8;
        }
        Console.Write(octalNum);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int n = 33;
 
        // Function Call
        octaltodecimal(n);
    }
}
 
// This code is contributed by rutvik_56


Javascript


Java
// JAVA program to convert decimal
// number to octal number
import java.io.*;
 
class GFG {
    public static void decToOct(int n)
    {
        System.out.println(Integer.toOctalString(n));
    }
    public static void main(String[] args)
    {
 
        int n = 33;
        decToOct(n);
    }
}


Python3
# Python program to convert decimal
# number to octal number
 
def decToOct(n):
    print(oct(n));
 
if __name__ == '__main__':
 
    n = 33;
    decToOct(n);
     
# This code is contributed by Amit Katiyar


C#
// C# program to convert decimal
// number to octal number
using System;
 
public class GFG
{
    public static void decToOct(int n)
    {
        Console.WriteLine(Convert.ToString(n, 8));
    }
    public static void Main(String[] args)
    {
 
        int n = 33;
        decToOct(n);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出
41

时间复杂度: O(log N)

另一种方法:(O(1)空间复杂度)

这个问题也可以在不使用数组的情况下使用以下算法解决:

  • 将 ocatalNum 初始化为 0,将 countVal 初始化为 1,将十进制数初始化为 n
  • 求十进制数除以8的余数
  • 通过octalNum + (remainder * countval) 更新八进制数
  • 将 countval 增加 countval*10
  • 十进制数除以 8
  • 从第二步开始重复,直到十进制数为零

下面是上述想法的实现:

C++

// C++ program to convert decimal
// number to octal number
#include 
using namespace std;
 
// function to calculate the octal value of the given
// decimal number
void decimaltoOctal(int deciNum)
{
 
    // initializations
    int octalNum = 0, countval = 1;
    int dNo = deciNum;
 
    while (deciNum != 0) {
 
        // decimals remainder is calculated
        int remainder = deciNum % 8;
 
        // storing the octalvalue
        octalNum += remainder * countval;
 
        // storing exponential value
        countval = countval * 10;
        deciNum /= 8;
    }
    cout << octalNum << endl;
}
 
// Driver Code
int main()
{
    int n = 33;
 
    // Function Call
    decimaltoOctal(n);
    return 0;
}

C

// C program to convert decimal
// number to octal number
#include 
 
// function to calculate the octal value of the given
// decimal number
void decimaltoOctal(int deciNum)
{
 
    int octalNum = 0, countval = 1;
    int dNo = deciNum;
 
    while (deciNum != 0) {
        // decimals remainder is calculated
        int remainder = deciNum % 8;
 
        // storing the octalvalue
        octalNum += remainder * countval;
 
        // storing exponential value
        countval = countval * 10;
        deciNum /= 8;
    }
    printf("%d", octalNum);
}
 
// Driver Code
int main()
{
    int n = 33;
 
    // Function Call
    decimaltoOctal(n);
    return 0;
}

Java

// JAVA program to convert decimal
// number to octal number
 
import java.io.*;
 
class GFG {
    // function to calculate the octal value of the given
    // decimal number
    static void octaltodecimal(int deciNum)
    {
        int octalNum = 0, countval = 1;
        int dNo = deciNum;
        while (deciNum != 0) {
 
            // decimals remainder is calculated
            int remainder = deciNum % 8;
 
            // storing the octalvalue
            octalNum += remainder * countval;
 
            // storing exponential value
            countval = countval * 10;
            deciNum /= 8;
        }
        System.out.println(octalNum);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 33;
 
        // Function Call
        octaltodecimal(n);
    }
}

蟒蛇3

# Python3 program to convert decimal
# number to octal number
 
# function to calculate the octal value of the given
# decimal number
 
 
def decimaltoOctal(deciNum):
 
    # initializations
    octalNum = 0
    countval = 1
    dNo = deciNum
 
    while (deciNum != 0):
 
        # decimals remainder is calculated
        remainder = deciNum % 8
 
        # storing the octalvalue
        octalNum += remainder * countval
 
        # storing exponential value
        countval = countval * 10
        deciNum //= 8
 
    print(octalNum)
 
 
# Driver Code
if __name__ == '__main__':
 
    n = 33
 
    # Function Call
    decimaltoOctal(n)
 
# This code is contributed by pratham76

C#

// C# program to convert decimal
// number to octal number
using System;
class GFG {
 
    // function to calculate
    // the octal value of the given
    // decimal number
    static void octaltodecimal(int deciNum)
    {
        int octalNum = 0, countval = 1;
 
        while (deciNum != 0) {
            // decimals remainder is
            // calculated
            int remainder = deciNum % 8;
 
            // storing the octalvalue
            octalNum += remainder * countval;
 
            // storing exponential value
            countval = countval * 10;
            deciNum /= 8;
        }
        Console.Write(octalNum);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int n = 33;
 
        // Function Call
        octaltodecimal(n);
    }
}
 
// This code is contributed by rutvik_56

Javascript


输出
41

时间复杂度: O(log N)

辅助空间: O(1)

使用预定义函数

Java

// JAVA program to convert decimal
// number to octal number
import java.io.*;
 
class GFG {
    public static void decToOct(int n)
    {
        System.out.println(Integer.toOctalString(n));
    }
    public static void main(String[] args)
    {
 
        int n = 33;
        decToOct(n);
    }
}

蟒蛇3

# Python program to convert decimal
# number to octal number
 
def decToOct(n):
    print(oct(n));
 
if __name__ == '__main__':
 
    n = 33;
    decToOct(n);
     
# This code is contributed by Amit Katiyar

C#

// C# program to convert decimal
// number to octal number
using System;
 
public class GFG
{
    public static void decToOct(int n)
    {
        Console.WriteLine(Convert.ToString(n, 8));
    }
    public static void Main(String[] args)
    {
 
        int n = 33;
        decToOct(n);
    }
}
 
// This code is contributed by 29AjayKumar

Javascript


输出
41