1. Introduction

The factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. Factorials have vast applications in mathematics, particularly in permutations, combinations, and statistical computations. In this guide, we’ll create an R program that calculates the factorial of a user-specified number.

2. Program Overview

The program will prompt the user to input a non-negative integer. 

It will then compute the factorial of this number and present the result to the user.

3. Code Program

# Function to calculate factorial of a number recursively
factorial <- function(n) {
    if (n <= 1) {
        return(1)
    } else {
        return(n * factorial(n-1))
    }
}

# Request the user for a non-negative integer
cat("Enter a non-negative integer: ")
num <- as.integer(readLines(n=1))

# Ensure the number is non-negative
if (num < 0) {
    cat("Error: Please enter a non-negative integer.\n")
} else {
    result <- factorial(num)
    cat("The factorial of", num, "is:", result, "\n")
}

Output:

Enter a non-negative integer: 5
The factorial of 5 is: 120

4. Step By Step Explanation

1. The program starts by defining a factorial function. This function calculates the factorial of a number using a recursive approach.

factorial <- function(n) {
    if (n <= 1) {
        return(1)
    } else {
        return(n * factorial(n-1))
    }
}

2. Within the function, the base case is established: if ( n ) is 0 or 1, the factorial is 1.

    if (n <= 1) {
        return(1)
    }

3. For values of ( n ) greater than 1, the function calls itself, continually decreasing the value of ( n ) until it reaches the base case.

 return(n * factorial(n-1))

4. The main part of the program prompts the user to input a non-negative integer using the cat function.

cat("Enter a non-negative integer: ")

5. The readLines(n=1) function captures the user’s input. Given its character nature, we employ as.integer to convert it to an integer, which is stored in the num variable.

num <- as.integer(readLines(n=1))

6. A check ensures the user has provided a non-negative number. If the user enters a negative number, an error message is displayed.

if (num < 0) {
    cat("Error: Please enter a non-negative integer.\n")
}

7. If the number is valid, the factorial function computes the factorial of the number.

    result <- factorial(num)

8. Finally, the result is displayed to the user using the cat function, showing the factorial of the provided number.

    cat("The factorial of", num, "is:", result, "\n")

factorial <- function(n) {
    if (n <= 1) {
        return(1)
    } else {
        return(n * factorial(n-1))
    }
}

# Request the user for a non-negative integer
cat("Enter a non-negative integer: ")
num <- as.integer(readLines(n=1))

# Ensure the number is non-negative
if (num < 0) {
    cat("Error: Please enter a non-negative integer.\n")
} else {
    result <- factorial(num)
    cat("The factorial of", num, "is:", result, "\n")
}