📚 Study/Java
JAVA :: Test049_반복문 while (누적합 연산 수행)
bono-hye
2023. 9. 9. 00:55
○ 실습
반복문을 활용하여 누적곱 연상 수행
1 * 2 * 3 * 4 * ... * 9 * 10
실행 예)
연산 결과 : 3628800
계속하려면 아무 키나 누르세요...
▼ 내가 작성한 코드
public class Test049
{
public static void main(String[] args)
{
int n = 0;
int multi = 1;
while (n<10)
{
n++;
multi *= n;
}
System.out.printf("연산 결과 : %d\n", multi);
}
}
▼ 같이 작성한 코드
public class Test049
{
public static void main(String[]args)
{
// 주요 변수 선언 및 초기화
int n = 0; //-- 루프 변수
int result = 1; //-- 누적곱을 담아낼 변수
//-- check~!!! (1로 초기화~!!!)
// 누적합을 처리할 때와 같이
// 0 으로 초기화를 수행하게 되면
// 어떤 수를 곱하더라도 연산 결과는 0
// 연산 및 처리 (반복문 구성)
while (n<10)
{
n++;
result *= n ;
}
//결과 출력
System.out.printf("연산 결과 : %d\n", result);