I am writing a code for the factorial function. My code is as follows:

> f<- function(n){+ factorial <- 1+ if( n < 0 )+ print("Factorial of negative 
> numbers is not possible")+ else if( n == 0 )+ print("Factorial of 0 is 1")+ 
> else {+ for(i in 1:n)+ factorial <- factorial * i+ print(paste("Factorial of 
> ",n," is ",factorial))+ }+ }

My problem with this code is for decimal numbers as input. For example for
f(6.5) my code computes 720, but we know 6.5 ! does not exist. For decimal
numbers like, or sqrt(2) I would like to see a message like

"The factorial for this number does not exist".

How can I fix this problem in my code?

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to