C++ program to find factorial using recursion - C++ explanation

Lets see an Important topic of C++ called as Recursion. In C/C++, a function can call itself that function is said to be recursive if a statement in the body of the function calls itself. Recursion is the process of defining something in terms of itself, and is sometimes called circular definition

Lets see an Important topic of C++ called as Recursion. In C/C++, a function can call itself that function is said to be recursive if a statement in the body of the function calls itself. Recursion is the process of defining something in terms of itself, and is sometimes called circular definition. Now we will see a C++ program to find factorial using class and recursion.

A very simple example of a recursive function is factr(), which computes the factorial of an integer. The factorial of a number n is the product of all the whole numbers between 1 and n.

 Recursion in C++

For example:  Factorial of 3 is 1 x 2 x 3 equals to 6. Lets see the program to understand better

## C++ program to find factorial using recursion

```#include <iostream>
using namespace std;

int factr(int n); /*functions prototype*/
int fact(int n) ;

int main(){
int n;
cout<<"Enter the no. To find factorial ";
cin>>n;
cout<<"function 1 (factr):"<<fact(n)<<endl;
cout<<"function 2 (fact):"<<fact(n);
return 0;
}

/* recursive call */
int factr(int n) {
if(n==1) return(1);
}

/* non-recursive */
int fact(int n) {
for(t=1; t<=n; t++)
}```

Output

 Recursion in C++ output

Explanation

Here I used two factorial functions which perform the same job but the non-recursive version of fact() should be clear. It uses a loop that runs from 1 to n and progressively multiplies each number by the moving product.
The operation of the recursive factr() is a little more complex. When factr() is called with an argument of 1, the function returns 1. Otherwise, it returns the product of factr(n−1)*n. To evaluate this expression, factr() is called with n−1. This happens until n equals 1 and the calls to the function begin returning.
When a function calls itself, a new set of local variables and parameters are allocated storage on the stack, and the function code is executed from the top with these new variables.
This video explains the recursion in best way: https://youtu.be/7TxCYUM173M

A recursive call does not make a new copy of the function. Only the values being operated upon are new. As each recursive call returns, the old local variables and parameters are removed from the stack and execution resumes at the point of the function call inside the function.
So now you understood the recursion in better way, If any problem persist add it in comment area below.

Name

ltr
item
MindxMaster: C++ program to find factorial using recursion - C++ explanation
C++ program to find factorial using recursion - C++ explanation
Lets see an Important topic of C++ called as Recursion. In C/C++, a function can call itself that function is said to be recursive if a statement in the body of the function calls itself. Recursion is the process of defining something in terms of itself, and is sometimes called circular definition
https://3.bp.blogspot.com/-aj_zuxUsN5Y/VfDrsxaG4WI/AAAAAAAAAoA/r0BUiRgU_6U/s1600/Unitled.png
https://3.bp.blogspot.com/-aj_zuxUsN5Y/VfDrsxaG4WI/AAAAAAAAAoA/r0BUiRgU_6U/s72-c/Unitled.png
MindxMaster
https://www.mindxmaster.com/2015/09/factorial-using-recursion-in-c.html
https://www.mindxmaster.com/
https://www.mindxmaster.com/
https://www.mindxmaster.com/2015/09/factorial-using-recursion-in-c.html
true
5332415103371288268
UTF-8