Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at a Scala program to find Factorial of a given number using Linear Recursion.
Let’s look at the program:
1 2 3 4 5 6 7 8 9 10 |
package com.clearurdoubt object Factorial extends App { def factorial(n: Int) : Int = { if (n == 0) 1 else n * factorial(n-1) // Linear Recursion } println(factorial(10)) } |
Output:
Currently this program uses Linear Recursion for finding the factorial. This program can be improved using Tail Recursion concept. Check out the below link:
Scala program to find Factorial of a given number using Tail Recursion
Happy Learning.
Please leave a reply in case of any queries.