From 82850c84e73eada4b7004458d2c06ecb6b0a561e Mon Sep 17 00:00:00 2001 From: Fatih <budakf@itu.edu.tr> Date: Sat, 19 Oct 2019 19:46:10 +0300 Subject: [PATCH] added tail recursion implementation of factorial --- .../C++/factorial_with_tail_resursion.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Maths/Factorial/C++/factorial_with_tail_resursion.cpp diff --git a/Maths/Factorial/C++/factorial_with_tail_resursion.cpp b/Maths/Factorial/C++/factorial_with_tail_resursion.cpp new file mode 100644 index 00000000..cd965131 --- /dev/null +++ b/Maths/Factorial/C++/factorial_with_tail_resursion.cpp @@ -0,0 +1,15 @@ +#include <iostream> + +long long int factorial_with_tail_recursion(long long int number, long long int accumulator=1 ){ + if(number<=0) + return accumulator; + else + return factorial_with_tail_recursion(number-1, accumulator*number); +} + +int main(){ + std::cout<<factorial_with_tail_recursion(6)<<std::endl; + std::cout<<factorial_with_tail_recursion(7)<<std::endl; + std::cout<<factorial_with_tail_recursion(8)<<std::endl; + return 0; +} \ No newline at end of file