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
- Find the last element of a list in scala
- Scala Iterator find() method with example
- Set in Scala | Set-2
- Scala Map
- Set in Scala | Set-1
- Scala | Either
- Scala | Arrays
- Scala | Traits
- Stack in Scala
- How to sort a Scala Map by value
- How to sort a Scala Map by key
- Scala vs Java
- Scala Varargs
- Inner class in Scala
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.