반복문 연습하기 Part.1~3

코등어·2024년 11월 25일
0

사전캠프

목록 보기
12/19

1. 1부터 100까지의 숫자 출력하기

public class Main {
    public static void main(String[] args) {
    	 for (int i = 1; i <= 100; i++) {
            System.out.print(i);
            if(i < 100){
                System.out.print(", ");
            }
            if(i % 10 == 0){
                System.out.println("");
            }
        }
     }
 }

1부터 100까지 숫자들을 한눈에 쉽게 보기 위해서 10개씩 끊어서 출력했습니다.

2. 1부터 100까지의 짝수만 출력하기

public class Main {
    public static void main(String[] args) {
    	  for (int i = 1,  count = 0; i <= 100; i++) {
            if(i % 2 == 0){
                count++;
                if(count % 10 == 0){
                    System.out.print(i);
                    System.out.println();
                }else {
                    System.out.print(i+ ", ");
                }
            }
        }
    }
}

1번과 같이 1부터 100까지 짝수들을 ','로 구분하고 10개씩 끊어 출력했습니다.

3. 구구단 출력하기

public class Main {
    public static void main(String[] args) {
        for (int j = 2; j <= 9; j++) {
            System.out.print("== "+j+ "단 ==\t");
        }
        System.out.println();
        for (int i = 1; i <= 9;i++) {
            for (int j = 2; j <= 9; j++) {
                System.out.print(j + " * " + i + " = " + (i*j) + "\t");
            }
            System.out.println();
        }
    }
}

이번에는 구구단을 가로로 출력 했습니다.

profile
정형화되지 않은 날 것의 생각을 기록합니다.

0개의 댓글