Lab 3: Recursive Functions How recursion works in C++ ? void recurse() \& ....... (): recurse(): int maind) recurse(); ?........ void recurse() \& recurse(): ?) int main() \& recurse(); ? I. Examples of recursive functions: Example 1: Factorials: To obtain the factorial of a number (n1) the mathematical formula would be: n!=n?(n?1)?(n?2)?(n?3)…?1 More concretely, 5 ( (factorial of 5) would be: 5!?5=4=3!2=1=120 and a recursive function to calculate this in C++ could be: II factorial calculator Please type a number: 9 finciude 91=362880 using namespace std; long factorial (long a). i. If (a?11 return (a + factorial (a?1)) : elae return (2): int main () long number: cout « "please type a number: "? cin 2> number: cout << number <?"n=?c< factorial (numbox) : teturn 0 ;
Example 2: Fibonacci numbers: The mathematical definition of the Fibonacci number is: fib(0)=1fib(1)=1fib(n)=fib(n?1)+fib(n?2)? As with factorial, this maps very cleanly into a recursive procedure: (1) Etbonacei number: Inelude F?b(4)=5. using namerpace std; Int fib(1nt n)./1,1,2,3,5, 8. 13,21,…+ 1f in ?0.11n=?1) return 1 ; retura fib(n?1)+fib(n?2); ) int main() if int n; cout << "Bnter a non-negative integer: ?; cos3n : Eib(n) ce ". " C++ program to find sum of first n natural numbers using recursion. Note: Positive integers are known as natural number i,e,1,2,3…
Exercise 1: Write a C++ Program for calculating the 5?n by using recursive functions. Exercise 2: Write a C+ Program to check whether a number is a palindrome or not. Note: we can think of a palindrome as just any sequence of letters or numbers that reads the same forward and backward, such as yzxzy or 12321 Exercise 3: Write a CH program using recursion to print the following table (called Pascal's Triangle) in your output screen