Recursion Examples Set 3
Set 1 Set 2 Set 3
Question : 5
Write a program in C langauge to take input for a limit and print fibonacci series upto the limit using recursion?
#include<stdio.h>
#include<conio.h>
void fibo(int n,int f3)
{
static int f1=0,f2=1;
f3=f1+f2;
if(f3<=n)
{
printf(“%d “,f3);
f1=f2;
f2=f3;
fibo(n,f3);
}
else
return;
}
int main()
{
int n;
printf(“Enter any limit “);
scanf(“%d”,&n);
/* if we want 0 1 should also come at start*/
printf(“0 1 “);
/* else not */
fibo(n,1);
return(0);
}
Question : 6
Write a program in C langauge to take input for two non negative integers calculate and print their sum using recursion?
/*
to calculate and print the sum of two
non negetive integers*/
#include<stdio.h>
#include<conio.h>
int sum(int a,int b)
{
if (a==0)
return(b);
else
sum(–a,++b);
}
int main()
{
int a,b,res;
printf(“enter any two number “);
scanf(“%d %d”,&a,&b);
res=sum(a,b);
printf(“the sum of %d and %d is %d\n”,a,b,res);
return(0);
}