Bibi's DevLog ๐Ÿค“๐ŸŽ

[๋ฐฑ์ค€ 10872] ํŒฉํ† ๋ฆฌ์–ผ (Swift) - ์žฌ๊ท€ ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด Swift

[๋ฐฑ์ค€ 10872] ํŒฉํ† ๋ฆฌ์–ผ (Swift) - ์žฌ๊ท€

๋น„๋น„ bibi 2023. 1. 30. 17:09

10872๋ฒˆ: ํŒฉํ† ๋ฆฌ์–ผ

ํŒฉํ† ๋ฆฌ์–ผ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์ด๋‹ค.

ํŒฉํ† ๋ฆฌ์–ผ์˜ ์ •์˜๋ฅผ ๋ณด๊ณ  ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค.

  • 0! = 1
  • 1! = 1
  • 2! = 2 * 1 = 2
  • 3! = 3 * 2 * 1 = 6
  • 4! = 4 * 3 * 2 * 1 = 24
  • โ€ฆ

๋ฐ˜๋ณต๋ฌธ์„ ์“ธ๊นŒ ํ•˜๋‹ค๊ฐ€ ์ด๋ฒˆ์—๋Š” ์žฌ๊ท€๋ฅผ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด ๋ณด์•˜๋‹ค.

ํ’€์ด

let input = Int(readLine()!)!
print(factorial(num: input))

func factorial(num: Int) -> Int {
    if num == 0 || num == 1 {
        return 1
    }
    return num * factorial(num: num - 1)
}