Demonstrate the technique of recursion in C Recursion – Write recursive function for
- Sum of natural numbers
- Factorial of a given number
- Fibonacci sequence
Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.
This implies, the caller function has to suspend its execution temporarily and resume later when the execution control returns from the callee function. Here, the caller function needs to start exactly from the point of execution where it puts itself on hold. It also needs the exact same data values it was working on. For this purpose, an activation record (or stack frame) is created for the caller function.
#include<stdio.h> #include<conio.h>
int fib(int n)
{
int x;
if (n==1)
return 0; else if (n==2) return 1;
x=fib(n-1) + fib(n-2); return (x);
}
int sum(int n)
{
if (n<=1)
return 1; return n+sum(n-1);
}
int factorial(int n)
{
if (n<=1)
return 1;
return n*factorial(n-1);
}
void main()
{
int a[20],n, i, sum1, fact;
printf("enter the value for n:\n"); scanf("%d", &n);
for (i=1 ;i<= n;i++) a[i]=fib(i);
printf("\nfibonacci series\n "); for (i=1;i<= n;i++)
printf (" -->%d", a[i]);
printf("\n sum of natural numbers = "); sum1 = sum(n);
printf("%d", sum1);
printf("\n factorial of %d = ",n); fact = factorial(n);
printf("%d", fact);
getch();
}