영상
문제
package 반복문7_개념;
/*
[문제]
12와 15의 공배수를 작은것부터
순서대로 5개만 출력하시오.
[정답]
60, 120, 180, 240, 300
*/
public class 반복문7_개념01_공배수_문제 {
public static void main(String[] args) {
}
}
Java
복사
해설
package 반복문7_개념;
/*
[문제]
12와 15의 공배수를 작은것부터
순서대로 5개만 출력하시오.
[정답]
60, 120, 180, 240, 300
*/
public class 반복문7_개념01_공배수_정답 {
public static void main(String[] args) {
int x = 12;
int y = 15;
int i = y;
int count = 0;
boolean run = true;
while(run) {
if(i % x == 0 && i % y == 0) {
System.out.print(i + " ");
count += 1;
if(count == 5) {
run = false;
}
}
i += 1;
}
/*
int i = y;
for(int count=1; count<=5; count++) {
if(i % x == 0 && i % y == 0) {
System.out.print(i + " ");
} else {
System.out.print(i + " ");
count--;
}
i += 1;
}
*/
/*
int count = 1;
int i = y;
while(count <= 5) {
if(i % x == 0 && i % y == 0) {
System.out.print(i + " ");
count += 1;
}
i += 1;
}
*/
}
}
Java
복사