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

📅  最后修改于: 2022-05-13 01:55:46.169000             🧑  作者: Mango

十进制到八进制转换的Java程序

给定一个十进制数N,将N转换成等价的八进制数,即将基数为10的数转换为基数8。十进制数系统使用0-9的10位数字,八进制数系统使用0-7的8位数字表示任何数值。

插图:

Input : 33
Output: 41

Input : 10
Output: 12

方法一:

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

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

例子:

Java
// Java program to convert a Decimal Number to Octal Number
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method
    // To convert decimal to octal
    static void decToOctal(int n)
    {
        // Creating an Integer array of
        // 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]);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input Integer number
        int n = 33;
 
        // Calling the above method to convert
        // Decimal to Octal number
        decToOctal(n);
    }
}


Java
// Java Program to Convert Decimal Number to Octal Number
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // To calculate the octal value of the given
    // decimal number
    static void decimaltooctal(int deciNum)
    {
 
        // Initially declaring and initializing the
        // octal number with zero
        int octalNum = 0, countval = 1;
        int dNo = deciNum;
 
        // Condition check
        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;
        }
 
        // Print and display the octal number
        System.out.println(octalNum);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input decimal number
        int n = 33;
 
        // Calling the Method1 to convert above number
        // to Octal number system
        decimaltooctal(n);
    }
}





输出
41

方法二:

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

例子:

Java

// Java Program to Convert Decimal Number to Octal Number
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // To calculate the octal value of the given
    // decimal number
    static void decimaltooctal(int deciNum)
    {
 
        // Initially declaring and initializing the
        // octal number with zero
        int octalNum = 0, countval = 1;
        int dNo = deciNum;
 
        // Condition check
        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;
        }
 
        // Print and display the octal number
        System.out.println(octalNum);
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Custom input decimal number
        int n = 33;
 
        // Calling the Method1 to convert above number
        // to Octal number system
        decimaltooctal(n);
    }
}


输出
41

时间复杂度: O(log N)
辅助空间:O(1)