예제
<script>
/*
[문제]
200부터 250까지의 숫자중에서 5의 배수를 각각의 자리로 숫자를 나눈 후,
2 또는 5의 개수가 두 개인 숫자만 출력하시오.
*/
/*
[출력예시]
205 2
215 2
220 2
235 2
245 2
250 2
*/
</script>
HTML
복사
정답
<script>
/*
[문제]
200부터 250까지의 숫자중에서 5의 배수를 각각의 자리로 숫자를 나눈 후,
2 또는 5의 개수가 두 개인 숫자만 출력하시오.
*/
/*
[출력예시]
205 2
215 2
220 2
235 2
245 2
250 2
*/
// [for문]
let count = 0;
for(let i = 200; i <= 250; i++) {
if(i % 5 == 0) {
let _100 = parseInt(i / 100);
let _10 = parseInt(i % 100 / 10);
let _1 = i % 10;
if(_100 == 2 || _100 == 5) {
count += 1;
}
if(_10 == 2 || _10 == 5) {
count += 1;
}
if(_1 == 2 || _1 == 5) {
count += 1;
}
if(count == 2) {
document.write(i, " ", count, "<br>");
}
}
count = 0;
}
document.write("<br>");
// [while문]
let count2 = 0;
let i = 200;
while(i <= 250) {
if(i % 5 == 0) {
let _100 = parseInt(i / 100);
let _10 = parseInt(i % 100 / 10);
let _1 = i % 10;
if(_100 == 2 || _100 == 5) {
count2 += 1;
}
if(_10 == 2 || _10 == 5) {
count2 += 1;
}
if(_1 == 2 || _1 == 5) {
count2 += 1;
}
if(count2 == 2) {
document.write(i, " ", count2, "<br>");
}
}
count2 = 0;
i += 1;
}
</script>
HTML
복사


