본문 바로가기
💻 하나씩 차곡차곡/자료구조 & 알고리즘(JAVA)

이중루프를 사용하여 구구단 곱셈표 출력하기 (chap01/Multi99Table)

by 뚜루리 2022. 10. 13.
728x90
320x100
package chap01;

public class Multi99Table {

    // 이중루프로 구구단 곱셈표를 출력
    public static void main(String[] args) {

        System.out.println("----- 구구단 곱셈표 -----");

        for (int i = 1; i <=9 ; i++){
            for (int j = 1; j <= 9; j++){
                System.out.printf("%3d", i * j);
            }
            System.out.println();
        }

    }
}

 

응용하기

1) 위쪽과 왼쪽에 곱하는 수가 있는 구구단 곱셈표를 출력하는 프로그램을 작성하시오.

package chap01;

public class practice11 {

    // 위쪽과 왼쪽에 곱하는 수가 있는 구구단 곱셈표를 출력하는 프로그램을 작성하시오.
    public static void main(String[] args) {

        System.out.println("   | 1  2  3  4  5  6  7  8  9");
        System.out.println("---+--------------------------");

        for (int i = 1; i <=9 ; i++){
            System.out.print(i + " |");
            for (int j = 1; j <= 9; j++){
                System.out.printf("%3d", i * j);
            }
            System.out.println();
        }

    }
}

 

2) 구구단 곱셈표를 변형하여 곱셈이 아니라 덧셈을 출력하는 프로그램을 작성하시오.

package chap01;

public class practice12 {

    // 구구단 곱셈표를 변형하여 곱셈이 아니라 덧셈을 출력하는 프로그램을 작성하시오.
    // practice11과 같이 표의 위쪽과 왼쪽에 더하는 수를 출력하세요.
    public static void main(String[] args) {

        System.out.println("   | 1  2  3  4  5  6  7  8  9");
        System.out.println("---+--------------------------");

        for (int i = 1; i <=9 ; i++){
            System.out.print(i + " |");
            for (int j = 1; j <= 9; j++){
                System.out.printf("%3d", i + j);
            }
            System.out.println();
        }

    }
}

 

3) 입력한 수를 한 변으로 하는 정사각형을 *로 출력하는 프로그램을 작성하세요.

package chap01;

import java.util.Scanner;

public class practice13 {

    // 입력한 수를 한 변으로 하는 정사각형을 *로 출력하는 프로그램을 작성하세요.
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("정사각형을 출력합니다.");
        System.out.print("변의 길이 : ");
        int a = scan.nextInt();

        for (int i = 0; i < a ; i++){
            for (int j = 0; j < a; j++){
                System.out.print("*");
            }
            System.out.println();
        }

    }
}
728x90
320x100