Given a number N, the task is to calculate factorial of N.
In mathematics, the factorial of a positive integer N is the product of all positive integers less than or equal to N. The recursive formula to calculate factorial of a given positive integer N is
N! = N * ( N -1 )! N! = 1 if N = 1 or N = 0
Input : N = 3 Output : 6 Input : N = 5 Output : 120
Method 1 : Iterative Way
In this method, we will use a loop to iterate over the sequence of numbers to get the factorial.
Below is the implementation of above approach
Method 2 : Use of Recursion
In this method, Recursive formula N! = N * (N -1) ! is used to calculate the factorial of the given number.
Below is the implementation of above approach.
- Scala Map find() method with example
- How to find largest key in Scala Map
- Scala Iterator find() method with example
- Find the last element of a list in scala
- Set in Scala | Set-2
- Set in Scala | Set-1
- Scala | Either
- Scala Map
- Scala Varargs
- How to get all the values from a Scala map
- Comments In Scala
- Operators in Scala
- while and do while Loop in Scala
- Scala Extractors
- Scala For Comprehensions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.