๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Study/Java

[TIL] ์žฌ๊ท€ํ•จ์ˆ˜

by hong- 2022. 5. 24.

โ˜๐Ÿป ์žฌ๊ท€ํ•จ์ˆ˜

๐Ÿ’ก ์žฌ๊ท€ Recursion ?

     ๋ฌธ์ œ๋ฅผ ๋” ์ž‘๊ฒŒ ๋‚˜๋ˆ ๋ณด๊ณ  ๊ทธ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉฐ ์ „์ฒด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

  ๐ŸŒŸ ์ข…๋ฃŒ ์กฐ๊ฑด์ด ์žˆ์–ด์•ผ ํ•จ

    - ์ข…๋ฃŒ์กฐ๊ฑด์ด ์—†์œผ๋ฉด ์ž๊ธฐ ์ž์‹ ์„ ๊ณ„์† ํ˜ธ์ถœํ•˜์—ฌ ์—๋Ÿฌ ๋ฐœ์ƒ


๐Ÿ“ ํŒฉํ† ๋ฆฌ์–ผ ๋ฌธ์ œ

package code;

public class FactorialTest {
    public static void main(String[] args) {
        int input = 5;

        System.out.println(fact(input));
    }

    public static int fact(int n){
        if (n<=1){
            return  n;
        } else{
            return fact(n-1)*n;
        }
    }
}