How to find the factorial of a number using recursion in C++

2 Answers

0 votes
#include <iostream>
 
int factorial_recursion(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    else {
        return(n * factorial_recursion(n - 1));
    }
}
 
int main()
{
    std::cout << factorial_recursion(5);
}
 
 
 
/*
run:
 
120
 
*/

 



answered Jan 17, 2021 by avibootz
edited Mar 4, 2023 by avibootz
0 votes
#include <iostream>
 
int factorial_recursion(int n) {
    return n ? n * factorial_recursion(n - 1) : 1;
}
 
int main()
{
    std::cout << factorial_recursion(5);
}
 
 
 
/*
run:
 
120
 
*/

 



answered Mar 4, 2023 by avibootz

Related questions

2 answers 261 views
2 answers 205 views
1 answer 227 views
2 answers 177 views
1 answer 165 views
...