Java 程式將十進位制轉換為八進位制
在本文中,我們將瞭解如何將十進位制轉換為八進位制。十進位制數是由小數點分隔整數和小數部分的數。八進位制數的基數為八,使用 0 到 7 之間的數字。
下面是對同一內容的演示 −
輸入
假設我們的輸入為 −
Enter the decimal number : 8
輸出
所需的輸出為 −
The octal value is 10
演算法
Step 1 - START Step 2 - Declare three integer value namely my_input, I and j and an integer array my_octal Step 3 - Read the required values from the user/ define the values Step 4 – Using a while condition of input not equal to 0, compute my_input % 8 and store it to my_octal[i] Step 5 - Compute my_input / 8 and assign it to ‘my_input’, increment ‘i’ value. Step 6 – Iterating using a for loop, print the ‘my_octal’ array Step 7- Stop
示例 1
此處,使用者根據提示輸入。你可以在我們的編碼工具
中即時試用此示例。
import java.util.Scanner;
public class DecimalToOctal {
public static void main(String[] args){
int my_input, i, j;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the decimal number : ");
my_input = my_scanner.nextInt();
int[] my_octal = new int[100];
System.out.println("The octal value is ");
i = 0;
while (my_input != 0) {
my_octal[i] = my_input % 8;
my_input = my_input / 8;
i++;
}
for ( j = i - 1; j >= 0; j--)
System.out.print(my_octal[j]);
}
}輸出
Required packages have been imported A reader object has been defined Enter the decimal number : 8 The octal value is 10
示例 2
此處,該整數已經事先定義,並且獲取其值並顯示在控制檯中。
public class DecimalToOctal {
public static void main(String[] args){
int my_input, i, j;
my_input = 8;
System.out.println("The decimal number is defined as " +my_input);
int[] my_octal = new int[100];
System.out.println("The octal value is ");
i = 0;
while (my_input != 0) {
my_octal[i] = my_input % 8;
my_input = my_input / 8;
i++;
}
for ( j = i - 1; j >= 0; j--)
System.out.print(my_octal[j]);
}
}輸出
The decimal number is defined as 8 The octal value is 10
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP