🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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")
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment