Recursion Examples Set 2
Set 1 Set 2 Set 3
Question : 3
Write a program in C langauge to calculate and print product of all the numbers upto the limit using recursion?
#include<stdio.h>
#include<conio.h>
int prod(int n)
{
if (n==0)
return(1);
else
return(n*prod(n-1));
}
int main()
{
int n,s;
printf(“Enter any limit number “);
scanf(“%d”,&n);
s=prod(n);
printf(“the prod of the first %d numbers is %d \n”,n,s);
return(0);
}
Question : 4
Write a program in C langauge to take input for a number calculate and print its table using recursion?
#include<stdio.h>
#include<conio.h>
void table(int n,int i)
{
if (i>10)
return;
else
{
//printf(“%d\n”,i*n);
printf(“%d * %d = %d\n”,n,i,n*i);
table(n,i+1);
}
}
int main()
{
int n,i=1;
printf(“Enter any number “);
scanf(“%d”,&n);
table(n,i);
return(0);
}