Search

반복문3_문제01_약수_누적

대분류
STEP03 조건문/일차반복문
문제 난이도
LV02
소분류
일차반복문3_문제

영상

문제

<script> /* [문제] [조건1] 30의 약수를 출력하고 [조건2] 전체 합을 구하시오. [조건3] 개수를 구하시오. [정답] 1 2 3 5 6 10 15 30 total = 72 count = 8 */ </script>
Java
복사

해설

<script> /* [문제] [조건1] 30의 약수를 출력하고 [조건2] 전체 합을 구하시오. [조건3] 개수를 구하시오. [정답] 1 2 3 5 6 10 15 30 total = 72 count = 8 */ let num = 30; let total = 0; let count = 0; let i = 1; while(i <= num) { if(num % i == 0) { console.log(i); document.write(i + "&nbsp;"); total += i; count += 1; } i += 1; } console.log("total = " + total); console.log("count = " + count); document.write("<br>"); document.write("total = " + total + "<br>"); document.write("count = " + count); </script>
Java
복사