영상
개념
<script>
/*
[문제]
랜덤 숫자 100~200을 arr배열에 다섯 개 저장하고,
다섯 개 숫자 중에 3의 배수들을 출력하시오.
3의 배수들의 개수와
3의 배수들의 누적 합도 출력하시오.
[예시]
arr = 172,148,136,192,108
192 108
3의 배수의 개수 = 2
3의 배수의 합 = 300
*/
let arr = [];
for(let i=0; i<5; i++) {
arr.push(Math.floor(Math.random() * 101) + 100);
}
document.write("arr = " + arr + "<br>");
let count = 0;
let total = 0;
for(let i=0; i<5; i++) {
if(arr[i] % 3 == 0) {
document.write(arr[i] + " ");
count += 1;
total += arr[i];
}
}
document.write("<br>");
document.write("3의 배수의 개수 = " + count + "<br>");
document.write("3의 배수의 합 = " + total);
</script>
Java
복사