EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Factorial in R
Secondary Sidebar
R programming Tutorial
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

Factorial in R

By Afshan BanuAfshan Banu

Factorial in R

Introduction to Factorial in R

A mathematical concept which is based on the idea of calculation of product of a number from one to the specified number, with multiplication working in reverse order i.e. starting from the number to one, and is common in permutations and combinations and probability theory, which can be implemented very effectively through R programming either through user-defined functions or by making use of an in-built function, is known as factorial in R programming.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

Let us see some examples to find factorial –

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The factorial of 0 = 0! = 1.

The factorial of 1 = 1! = 1.

The factorial of 2 = 2! = n* ( n – 1)= 2* ( 2 – 1) = 2 * 1 = 2.

The factorial of 3 = 3! = n* ( n – 1) * (n – 2)= 3* ( 3 – 1)* (3 – 2) = 3 * 2 * 1 = 6.

The factorial of 4 = 4! = n* ( n – 1) * (n – 2)* (n – 3)= 4* ( 4 – 1)* (4 – 2)* (4 – 3) =4* 3 * 2 * 1 = 24.

The factorial of 5 = 5! = n* ( n – 1) * (n – 2)* (n – 3)* (n – 4) = 5* ( 5 – 1)* (5 – 2)* (5 – 3)* (5 – 4) =5 * 4 * 3 * 2 * 1 = 120. And so on.

As in the above calculation, we have seen that the factorial of 0 is 1, whereas the factorial of the negative number is not defined, in R we get NAN as the output for factorial of the negative number.

How to Find Factorial in R Programming?

Here we will discuss the program to calculate the factorial using various methods.

Example #1 – Factorial using if-else statement

Code:

facto <- function(){
# accept the input provided by the user and convert to integer
no = as.integer( readline(" Input a number to find factorial : "))
fact = 1
# checking whether the number is negative, zero or positive
if(no < 0) {
print(" The number is negative the factorial does not exist. ")
} else if(no == 0) {
print(" The factorial result is 1 ")
} else {
for( i in 1:no) {
fact = fact * i
}
print(paste(" The factorial result is ", no ,"is", fact ))
}
}
facto()

Output:

input number

The output of the above code for negative number–

negative factorial

In the above code the if-else statement first check whether the no is negative or not, if the no is negative means no < 0 condition is true then output display “ The number is negative the factorial does not exist ”, Whereas if condition is false then the else if no == 0 condition checks, if its true the output display “The factorial is 1”, else with the for loop calculate the factorial and display calculated value as output.

Example #2 – Factorial using for loop

Code:

facto <- function(){
no = as.integer( readline(prompt=" Enter a number to find factorial : "))
fact = 1
for( i in 1:no) {
fact = fact * i
}
print(paste(" The factorial of ", no ,"is", fact ))
}
facto()

Output:

factorial

In the above code, it is just finding the factorial without checking whether the number is negative or not.

Example #3 – Factorial using recursion Method

Code:

fact <- function( no ) {
# check if no negative, zero or one then return 1
if( no <= 1) {
return(1)
} else {
return(no * fact(no-1))
}
}

Output:

Recursion method

The output of the above code for negative number–

fact of r

The output of the above code for zero number–

Factorial in R

The output of the above code for positive number–

fact 10

The above code using the recursive function fact( ), inside the fact( ) function the factorial is finding by product of the each number recursively by the line return(no * fact(no-1)). Suppose we call fact function as fact(7) then the function fact() recursively as given below –

no = 7

if (no <= 1) -> false

return no * fact(no-1) => 7 * fact(6) => 7 * 6 * fact(5) => => 7 * 6 * 5 * fact(4) => 7 * 6 * 5 * 4 * fact(3) => 7 * 6 * 5 * 4 * 3 * fact(2) => 7 * 6 * 5 * 4 * 3 * 2 * fact(1) => 7 * 6 * 5 * 4 * 3 * 2 * 1 => 5040. So the final result is 5040.

Example #4 – Factorial using the built-in function

The factorial( ) function is the built-in function of R language which is used to calculate the factorial of a number. The syntax of the function is –

factorial( no )

no – numeric vector

Some of the example for factorial( no ) function with different parameters –

# find the factorial of -1
> factorial(-1)
[1] NaN
# find the factorial of 0
> factorial(0)
[1] 1
# find the factorial of 1
> factorial(1)
[1] 1
# find the factorial of 7
> factorial(7)
[1] 5040
# find the factorial for vector of each elements 2, 3, 4
> factorial(c(2,3,4))
[1]  2  6 24

Conclusion

  • The product of all the numbers from 1 to the specified number is called the factorial of a specified number.
  • The formula or logic used to find the factorial of n number is n! = n* ( n – 1)* ( n – 2)* ( n – 3)….
  • The factorial of 0 is 1, the factorial of all negative number is not defined in R it outputs NAN.
  • In R language the factorial of a number can be found in two ways one is using them for loop and another way is using recursion (call the function recursively).

Recommended Articles

This is a guide to Factorial in R. Here we discuss introduction of Factorial in R along with examples to calculate factorial using variety of methods. You can also go through our other suggested articles to learn more –

  1. Factorial in Python
  2. Factorial in C
  3. Reverse Number in C
  4. Factorial in PHP
  5. Recursive Function in JavaScript
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more