이 기사에서는 구구단을 인쇄하는 방법을 이해할 것입니다. 곱셈표는 for 루프를 사용하여 필요한 입력을 10번 반복하고 각 반복에서 입력 값에 1에서 10까지의 숫자를 곱하여 생성됩니다.
아래는 동일한 데모입니다 -
입력
입력이 -
라고 가정합니다.Input : 16
출력
원하는 출력은 -
The multiplication table of 16 is : 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 16 * 8 = 128 16 * 9 = 144 16 * 10 = 160
알고리즘
Step 1 – START Step 2 – Declare two integer values namely my_input and i. Step 3 - Read the required values from the user/ define the values Step 4 – Iterate from 1 to 10 using a for-loop, and in each iteration, multiply the numbers 1 to 10 with the input. Step 5- Display the resulting value in after each iteration. Step 6- Stop
예시 1
여기에서 입력은 프롬프트에 따라 사용자가 입력하고 있습니다. 우리코딩 그라운드 도구에서 이 예제를 라이브로 사용해 볼 수 있습니다.
.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
int my_input, i;
my_input = 16;
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 a number : ");
my_input = my_scanner.nextInt();
System.out.println("The multiplication table of " +my_input + " is :");
for(i = 1; i <= 10; ++i){
System.out.printf("%d * %d = %d \n", my_input, i, my_input * i);
}
}
} 출력
Required packages have been imported A reader object has been defined Enter a number : 16 The multiplication table of 16 is : 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 16 * 8 = 128 16 * 9 = 144 16 * 10 = 160
예시 2
여기서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.
public class MultiplicationTable {
public static void main(String[] args) {
int my_input, i;
my_input = 16;
System.out.println("The number is defined as " +my_input);
System.out.println("The multiplication table of " +my_input + " is :");
for(i = 1; i <= 10; ++i){
System.out.printf("%d * %d = %d \n", my_input, i, my_input * i);
}
}
} 출력
The number is defined as 16 The multiplication table of 16 is : 16 * 1 = 16 16 * 2 = 32 16 * 3 = 48 16 * 4 = 64 16 * 5 = 80 16 * 6 = 96 16 * 7 = 112 16 * 8 = 128 16 * 9 = 144 16 * 10 = 160