Calculate Factorial of a value in R Programming - factorial() Function Last Updated : 25 Sep, 2023 Comments Improve Suggest changes Like Article Like Report The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. It is defined as: n! = n × (n - 1) × (n - 2) × ... × 2 × 1For example 5! = 5 × 4 × 3 × 2 × 1 = 1200! is defined to be 1.Factorials have important applications in mathematics, combinatorics, and probability theory. Method 1: Using Factorial() FunctionR Programming Language offers a factorial() a function that can compute the factorial of a number without writing the whole code for computing factorial. Syntax: factorial(x) Parameters:x: The number whose factorial has to be computed. Returns: The factorial of the desired number. Example 1 R # R program to calculate factorial value # Using factorial() method answer1 <- factorial(4) answer2 <- factorial(-3) answer3 <- factorial(0) print(answer1) print(answer2) print(answer3) Output: 24 NaN 1Example 2 Compute the factorial of multiple values R # R program to calculate factorial value # Using factorial() method answer1 <- factorial(c(0, 1, 2, 3, 4)) print(answer1) Output: 1 1 2 6 24Method 2: Finding Factorial Value Using If Else conditionWe can create a program to find the factorial of a given number using R if-else conditions. Steps:readline(prompt="Enter a number: ") reads a line of text from the user and displays the prompt "Enter a number: ".as.integer() is used to convert the user's input to an integer and store it in the variable num.The initial factorial is initialized to 1.if-else structure to check the value of numIf num is less than 0 (negative), it prints "Not possible for negative numbers."If num is equal to 0, it prints "The factorial of 0 is 1."If num is positive (greater than 0), it enters the else block to calculate the factorial.Inside the else block, there is a for loop that iterates from 1 to num.In each iteration, the factorial is updated by multiplying it by the current value of i. This loop calculates the factorial of the positive number.After the loop completes, it prints the result using print() and paste() functions, displaying the calculated factorial along with a message. R # take input from the user num = as.integer(readline(prompt="Enter a number: ")) factorial = 1 # check is the number is negative or possitive if(num < 0) { print("Not possible for negative numbers") } else if(num == 0) { print("The factorial of 0 is 1") } else { for(i in 1:num) { factorial = factorial * i } print(paste("The factorial of", num ,"is",factorial)) } Output: [1] "The factorial of 6 is 720" Comment More infoAdvertise with us Next Article Calculate Factorial of a value in R Programming - factorial() Function spp____ Follow Improve Article Tags : R Language R Math-Function Similar Reads Calculate nCr value in R Programming - choose() Function R Language offers a direct function that can compute the nCr value without writing the whole code for computing nCr value. Syntax: choose(n, r) Parameters: n: Number of elements r: Number of combinations Returns: The number of r combinations from a total of n elements, i.e, nCr value. Example 1: Pyt 1 min read Convert a Vector into Factor in R Programming - as.factor() Function as.factor() function in R Programming Language is used to convert the passed object(usually Vector) into a Factor. Syntax: as.factor(object) Parameters: Object: Vector to be convertedas.factor() Function in R ExampleExample 1: Convert a Factor in RR # Creating a vector x<-c("female", "male", "ma 1 min read Calculate exponential of a number in R Programming - exp() Function exp() function in R Language is used to calculate the power of e i.e. e^y or we can say exponential of y. The value of e is approximately equal to 2.71828â¦.. Syntax: exp(y) Parameters: y: It is any valid R number either positive or negative. Returns: Floating point number by calculating e^y. Example 1 min read Calculating Natural Logarithm of calculated nCr value in R Programming - lchoose() Function lchoose() function in R Language is used to return the natural logarithm of nCr value. This function is equal to log(choose(x)). Syntax: lchoose(n, r) Parameters: n: Number of elements r: Number of combinations Example 1: Python3 # R program to illustrate # lchoose function # Calling lchoose() funct 1 min read Compute the Natural Logarithm of Factorial in R Programming - lfactorial() Function lfactorial() function in R Language is used to compute the natural logarithm of factorial of x i.e, ln(x!). Syntax: lfactorial(x) Parameters: x: positive numeric value Example 1: Python3 # R program to illustrate # lfactorial function # Calling the lfactorial() function lfactorial(0) lfactorial(1) l 1 min read Compute the Exponential minus 1 of a Number in R Programming - expm1() Function expm1() function in R Language is used to compute exponential minus 1 i.e, exp()-1. Syntax: expm1(x) Parameters: a, b: number or number vector Example 1: Python3 # R program to illustrate # expm1 function # Calling the expm1() function expm1(4) # e5 -1 expm1(10) expm1(15) Output: [1] 53.59815 [1] 22 1 min read Compute the gamma value of a Non-negative Numeric Vector in R Programming - gamma() Function gamma() function in R Language is used to compute the gamma value of a numeric vector using the gamma function. Gamma function is basically, gamma(x) = factorial(x - 1) Syntax: gamma(x) Parameters: x: Non-negative Numeric Vector Example 1: Python3 1== # R program to calculate the gamma value # Calli 1 min read Convert Factor to Numeric and Numeric to Factor in R Programming Factors are data structures that are implemented to categorize the data or represent categorical data and store it on multiple levels. They can be stored as integers with a corresponding label to every unique integer. Though factors may look similar to character vectors, they are integers and care m 5 min read Calculate the Factorial of a Number using Loop In this article, we will discuss how to calculate the factorial of a number with its working example in the R Programming Language using R while loop. In R programming, loops are essential constructs that allow us to repeat a set of instructions multiple times. The while loop is one such construct t 3 min read Compute the Logarithmic Derivative of the gamma Function in R Programming - digamma() Function digamma() function in R Language is used to calculate the logarithmic derivative of the gamma value calculated using the gamma function. digamma Function is basically, digamma(x) = d(ln(factorial(n-1)))/dx Syntax: digamma(x) Parameters: x: Numeric vector Example 1: Python3 1== # R program to find lo 1 min read Like