개발/알고리즘 기초
[ 알고리즘 기초 ] 팩토리얼
s2somang
2021. 3. 29. 00:24
문제
입력한 수의 팩토리얼을 구하자!
5
=> 120
🥺 풀이 :
팩토리얼도 알고리즘에서 자주 볼수 있는 문제 같다..!
예를 들어 5의 팩토리얼을 구해보자.
5! = 5 * 4 * 3 * 2 * 1
간단하게 for,while같은 반복문을 이용해서 풀어주면 된다..!!
import java.util.Scanner;
public class 팩토리얼 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int nu, = sc.nextInt();
int result = 1;
for(int i =1; i<= num; i++) {
result *= i;
}
System.out.println(result);
}
}
<결과>
