Scala : Factorial using recursion



in this blog we will write a program in scala which will compute factorial of a given number, passed by value

following is the program


object factorial extends App {

def factorial(n:Int):Int= // define factorial function
def go(n:Int,acc:Int):Int= // define a inner function which is common in functional programming
if(n<=0) acc else go(n-1,acc*n) // recursion .. of go function
go(n,1) //calling function go
println(factorial(3)) // print factorial function output


output – 6 



Thanks . please comment and follow back


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s