Example 1 : (Direct Recursion )
void F1(....) { ... F1(......) ... } void main () { ... F1(); ... }
Example 2: (Indirect Recursion )
void F2(....) { ... F1(......) ... } void F1(....) { ... F2(......) ... } void main () { ... F1(); ... }
Recursion is an alternative way to repeat the function execution with the same or variant parameters.
Example 3 : Infinite Printing Loop
(3.a) Normal Loop :
Example 3 : Infinite Printing Loop
(3.a) Normal Loop :
#include <stdio.h> #include <stdlib.h> void printhello() { printf("Hello world\n"); } int main() { while(1) { printhello(); } return 0; }
(3.b) Recursive Loop :
Example 4 : Finite Printing Loop
(4.a) Finite Normal loop
(4.b) Finite Recursive Loop
#include <stdio.h> #include <stdlib.h> void printhello() { printf("Hello world \n"); printhello(); } int main() { printhello(); }
Example 4 : Finite Printing Loop
(4.a) Finite Normal loop
#include <stdio.h> #include <stdlib.h> void printhello() { printf("Hello world\n"); } int main() { int i=0; while(i<10) { printhello(); i++; } return 0; }
(4.b) Finite Recursive Loop
#include <stdio.h> #include <stdlib.h> void printhello(int counter) { if(counter > 0 ){ printf("Hello world \n"); counter--; } printhello(counter); } int main() { printhello(10); }
Example 5 : Multiplication Table by using Finite Recursive Loop
Output Sample
#include <stdio.h> #include <stdlib.h> void multi_table(int a) { static int counter=0; int y ; if(counter < 11){ y=counter*a; printf("%d * %d = %d\n",counter,a,y); counter++; multi_table(a); } } int main() { int x ; printf ("Please Enter An Integer Number \n"); scanf("%d",&x); printf("Multiplication Table Of %d\n",x); multi_table(x); }
Output Sample
0 Comment to "Recursion "
Post a Comment