[R] How to create a warning inside the factorial function for decimal numbers

Vahid Borji v@h|d@borj|65 @end|ng |rom gm@||@com
Mon Jun 1 12:49:16 CEST 2020


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]]



More information about the R-help mailing list