이 기사에서는 10진수를 8진수로 변환하는 방법을 이해할 것입니다. a 십진수는 정수 부분과 소수 부분이 소수점으로 구분되는 숫자입니다. 8진수는 8의 기수를 가지며 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