What's new

Help RECURSIVE FUNCTION LOGIC! (Explain why 40320 is the factorial of 8)

R3g4z4k1

Eternal Poster
Established
Joined
Jan 23, 2019
Posts
339
Reaction
2,417
Points
432
R Programming
#4 recursion
fact<-1
funct_recurse <- function(num)
{
print(paste("num :",num,"fact :",fact))
fact = 1
if(num > 1)
{ fact <- num * funct_recurse(num - 1)
print(paste("Num : ",num))
print(paste("Fact : ",fact))
return(fact)
}
else
{ return(fact)
}
}
number <- as.numeric(readline(prompt="input a number:"))
factorial <- funct_recurse(number)
print(paste("factorial of ",number,"is",factorial))


Output:
1669925686043.png
 

Attachments

Similar threads

Back
Top