Difference between Recursion and Iteration | C ++ program to find factorial of given number ; -- -- - Recursion -- -- - method to find factorial of given number ; recursion call ; -- -- - Iteration -- -- - Method to find the factorial of a given number ; using iteration ; Driver method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorialUsingRecursion ( int n ) { if ( n == 0 ) return 1 ; return n * factorialUsingRecursion ( n - 1 ) ; } int factorialUsingIteration ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } int main ( ) { int num = 5 ; cout << " Factorial ▁ of ▁ " << num << " ▁ using ▁ Recursion ▁ is : ▁ " << factorialUsingRecursion ( 5 ) << endl ; cout << " Factorial ▁ of ▁ " << num << " ▁ using ▁ Iteration ▁ is : ▁ " << factorialUsingIteration ( 5 ) ; return 0 ; }