🎓 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
Factorial is a fundamental concept in mathematics, denoted as n! (read as "n factorial"). It is the product of all positive integers from 1 to n. For instance, the factorial of 5 (5!) is 5 x 4 x 3 x 2 x 1 = 120. The factorial function grows rapidly, meaning factorials of even modestly large numbers are enormous. In this blog post, we will develop a C++ program that computes the factorial of a given number using recursion.
2. Program Overview
Recursion is a technique in programming where a function calls itself. For computing factorials, recursion proves useful. The factorial of 0 is 1, and the factorial of any positive number n is n multiplied by the factorial of n-1. We can express this as:
0! = 1 n! = n x (n-1)!
Our program will:
1. Prompt the user to enter a number.
2. Invoke a recursive function to compute the factorial.
3. Display the result.
3. Code Program
#include <iostream>
using namespace std;
// Recursive function to calculate factorial of a number
long long factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n <= 1)
return 1;
// Recursive case: n! = n x (n-1)!
else
return n * factorial(n - 1);
}
int main() {
int num;
// Prompt user for input
cout << "Enter a positive integer: ";
cin >> num;
// Display the result
cout << "Factorial of " << num << " = " << factorial(num);
return 0; // Indicate successful program termination
}
Output:
Enter a positive integer: 5 Factorial of 5 = 120
4. Step By Step Explanation
1. Headers and Namespace: We start by including the iostream library, essential for input-output operations, and declare the use of the standard namespace.
#include <iostream>
using namespace std;
2. Recursive Factorial Function: The function factorial calculates the factorial of an integer n. It has a base case for n values of 0 or 1, returning 1. For other values, it multiplies n with the factorial of n-1.
// Recursive function to calculate factorial of a number
long long factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n <= 1)
return 1;
// Recursive case: n! = n x (n-1)!
else
return n * factorial(n - 1);
}
3. Main Function: This is where the program begins its execution. We prompt the user for an integer input, compute the factorial using the recursive function, and then display the result.
int main() {
4. User Input: We use cout to prompt the user and cin to receive their input.
// Prompt user for input
cout << "Enter a positive integer: ";
cin >> num;
5. Factorial Calculation: We call the recursive function factorial to compute the value.
// Display the result
cout << "Factorial of " << num << " = " << factorial(num);
6. Program Termination: The program concludes its execution with a return value of 0.
return 0; // Indicate successful program termination
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