Recursion

Program to compute power of a given number x raised to y

Recursion Program to compute power of a given number x raised to y #include <stdio.h> #include <conio.h> int power(int x,int n) { if(n==0) return 1; else return(x * power(x,n-1)); } void main() { int x,n,res; clrscr(); printf(“nEnter base?”); scanf(“%d”,&x); printf(“nEnter exponent?”); scanf(“%d”,&n); res=power(x,n); printf(“Power of %d raised to %d is %d”,x,n,res); getch(); } Post Views: …

Program to compute power of a given number x raised to y Read More »

Program to compute GCD of given two numbers x and y

Recursion Program to compute GCD of given two numbers x and y #include <stdio.h> #include <conio.h> /*Recursive function to compute GCD*/ int GCD(int x,int y) { if(y==0) return x; else if(x<y) return GCD(y,x); else return GCD(y,x%y); } void main() { int x,y,res; clrscr(); printf(“nEnter first number?”); scanf(“%d”,&x); printf(“nEnter second number?”); scanf(“%d”,&y); res=GCD(x,y); printf(“nGCD of %d …

Program to compute GCD of given two numbers x and y Read More »

Program to solve Towers of Hanoi problem

Recursion Program to solve Towers of Hanoi problem #include <stdio.h> #include <conio.h> void hanoi(int num_disks,char *src,char *tmp,char *trgt); void main() { int disks; char source[10],temp[10],target[10]; clrscr(); printf(“nEnter total number of disks?”); scanf(“%d”,&disks); printf(“nEnter source pillar?”); scanf(“%s”,source); printf(“nEnter intermediate pillar?”); scanf(“%s”,temp); printf(“nEnter target pillar?”); scanf(“%s”,target); hanoi(disks,source,temp,target); getch(); } void hanoi(int num_disks,char *src,char *tmp,char *trgt) { if(num_disks==1) …

Program to solve Towers of Hanoi problem Read More »

Program to compute factorial of a given number

Recursion Program to compute factorial of a given number #include <stdio.h> #include <conio.h> /*Recursive Definition*/ /*int factorial(int n) {if(n==0) return 1; else return(n*factorial(n-1)); } */ /*Non-Recursive Definition*/ int factorial(int n) {int i,res=1; for(i=n;i>=1;i–) res=res*i; return(res); } void main() { int num,res; clrscr(); printf(“nEnter number to compute factorial?”); scanf(“%d”,&num); res=factorial(num); printf(“nFactorial = %d”,res); getch(); } Post …

Program to compute factorial of a given number Read More »

Recursion

Recursion A function call is said to be recursive, if we invoke a function from within the same function.  The phenomenon being referred to as recursion.  A termination condition must be specified with each recursive call to avoid infinite execution of that function.  Recursion is basically required whenever a function has to operate upon different …

Recursion Read More »

You cannot copy content of this page.