JAVA/Practice Q
Q12. 구구단의 짝수 단(2, 4, 6, 8단)만 출력하는 프로그램을 작성하되, 2단은 2X2까지, 4단은 4X4까지, 6단은 6X6까지 8단은 8X8까지만 출력하도록 구현하자.
민지짱민지최고
2023. 9. 3. 21:15
// 방법01. 문제 그대로
int x = 2;
while (x <= 9) {
if (x % 2 == 0) {
int y = 1;
while (y <= x) {
int result = x * y;
System.out.println(x + " X " + y + " = " + result);
y++;
}
}
x++;
}
// 방법02. 약간 내 방식대로? 걍 +2씩 해도되잖아
System.out.println("======== 방법02 ==========");
int a = 2;
while (a <= 8) {
int y = 1;
while (y <= a) {
int result = a * y;
System.out.println(a + " X " + y + " = " + result);
y++;
}
a += 2;
}
// 방법03. for문 이용
System.out.println("======== 방법03 ==========");
for(int c = 2; c <= 8; c += 2) {
for (int d = 1; d <= c; d++) {
int result = c * d;
System.out.println(c + " X " + d + " = " + result);
}
}