Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself.
recursion();
/* function calls itself */
}
int main( ) {
recursion( );
}
While using recursion, programmers need to be
careful to define an exit condition from the function, otherwise it will go
into an infinite loop.
return 1;
}
return i
* factorial(i - 1);
}
int i =
12;
printf("Factorial of %d is %d\n", i, factorial(i));
return
0;
}
The following example generates the Fibonacci series for a given number using a recursive function –
return 0;
}
if(i ==
1) {
return 1;
}
return
fibonacci(i-1) + fibonacci(i-2);
}
for (i =
0; i < 10; i++) {
printf("%d\t\n", fibonacci(i));
}
return
0;
}
No comments:
Post a Comment