팩토리얼

han.user();·2023년 4월 2일
0

프로그래머스

목록 보기
29/87
post-thumbnail

public class Solution {
    public int solution(int n) {
    
        int i = 1; // i를 1로 초기화

        int factorial = 1; // factorial을 1로 초기화

        while (factorial <= n) { // 팩토리얼수가 n보다 커지면 반복문 종료
            i++; // i를 1 증가
            factorial *= i; // i가 1씩 올라가며 팩토리얼에 곱해나감
        }
        return i-1; // 반복문이 n을 넘친 상태로 종료됐으므로 i를 하나 줄여줘야함
    }
}
profile
I'm still hungry.

0개의 댓글