Notice
Recent Posts
Recent Comments
Link
Everything has an expiration date
049 - Java 반복문 while을 이용하여 누적곱 연산 결과를 출력하는 프로그램 본문
[Java]/Program source (java)
049 - Java 반복문 while을 이용하여 누적곱 연산 결과를 출력하는 프로그램
Jelly-fish 2023. 8. 31. 12:03/*=========================================
■■■ 실행 흐름의 컨트롤(제어문) ■■■
- 반복문 실습 및 관찰
===========================================*/
// 반복문을 활용하여 누적곱 연산 수행
// cf. 누적합
// 1 * 2 * 3 * 4 * ... * 9 * 10
// 실행 예)
// 연산 결과 : xxxx
// 계속하려면 아무 키나 누르세요...
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.println("연산 결과 : ", result);
}
}
/*[내 풀이]===============================================================
public class Test049
{
public static void main(String[] args)
{
// 1. 변수 선언**********************
int i = 1;
int multResult = 1;
// 2. 연산 과정**********************
// → while 문을 통해 i가 1 ~ 10까지 증가하도록 하며, multResult에 그 값을 계속해서 곱해 나간다.
while (i < 11)
{
multResult *= i;
i++;
}
System.out.printf("연산 결과 : %d\n", multResult);
}
}
========================================================================*/