Difference between revisions of "Interview Preparation topic: Recursive Function"
Proj user18 (talk | contribs) |
Proj user18 (talk | contribs) |
||
Line 20: | Line 20: | ||
− | ''' | + | '''FACTORIAL OF A NUMBER''' |
Line 29: | Line 29: | ||
int factorial(unsigned int i) | int factorial(unsigned int i) | ||
− | { | + | { |
if(i <= 1) | if(i <= 1) | ||
− | { | + | { |
return 1; | return 1; | ||
− | } | + | } |
return i * factorial(i - 1); | return i * factorial(i - 1); | ||
− | } | + | } |
int main() | int main() | ||
− | { | + | { |
int i = 5; | int i = 5; | ||
printf("Factorial of %d is %d\n", i, factorial(i)); | printf("Factorial of %d is %d\n", i, factorial(i)); | ||
return 0; | return 0; | ||
− | } | + | } |
</pre> | </pre> | ||
Line 51: | Line 51: | ||
− | ''' | + | '''FIBONACCI SERIES''' |
Line 58: | Line 58: | ||
#include <stdio.h> | #include <stdio.h> | ||
int fibonacci(int i) | int fibonacci(int i) | ||
− | { | + | { |
if(i == 0) | if(i == 0) | ||
− | { | + | { |
return 0; | return 0; | ||
− | } | + | } |
if(i == 1) | if(i == 1) | ||
− | { | + | { |
return 1; | return 1; | ||
− | } | + | } |
return fibonacci(i-1) + fibonacci(i-2); | return fibonacci(i-1) + fibonacci(i-2); | ||
− | } | + | } |
int main() | int main() | ||
− | { | + | { |
int i; | int i; | ||
for (i = 0; i < 7; i++) | for (i = 0; i < 7; i++) | ||
− | { | + | { |
printf("%d\t\n", fibonacci(i)); | printf("%d\t\n", fibonacci(i)); | ||
− | } | + | } |
return 0; | return 0; | ||
− | } | + | } |
</pre> | </pre> | ||
Line 92: | Line 92: | ||
'''SUM OF NATURAL NUMBERS''' | '''SUM OF NATURAL NUMBERS''' | ||
+ | The following example calculates the sum of natural numbers using a recursive function: | ||
<pre> | <pre> | ||
#include <stdio.h> | #include <stdio.h> | ||
Line 98: | Line 99: | ||
int main() | int main() | ||
− | { | + | { |
− | int number, result; | + | int number, result; |
− | number = 5; | + | number = 5; |
− | result = sum(number); | + | result = sum(number); |
− | printf("Sum of %d Natural Numbers Using Recursion is %d\n",number, result); | + | printf("Sum of %d Natural Numbers Using Recursion is %d\n",number, result); |
− | } | + | } |
int sum(int num) | int sum(int num) | ||
− | { | + | { |
− | if (num != 0) | + | if (num != 0) |
− | return num + sum(num - 1); // sum() function calls itself | + | return num + sum(num - 1); // sum() function calls itself |
− | else | + | else |
− | return num; | + | return num; |
− | } | + | } |
</pre> | </pre> | ||
Revision as of 20:53, 18 December 2016
Recursion is the process of repeating items in a self-similar way.
If a program allows you to call a function inside the same function, then it is called a recursive call of the function.
void recursion() { 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.
Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
FACTORIAL OF A NUMBER
The following example calculates the factorial of a given number using a recursive function:
#include <stdio.h> int factorial(unsigned int i) { if(i <= 1) { return 1; } return i * factorial(i - 1); } int main() { int i = 5; printf("Factorial of %d is %d\n", i, factorial(i)); return 0; }
When the above code is compiled and executed, it produces the following result:
Factorial of 5 is 120
FIBONACCI SERIES
The following example generates the Fibonacci series for a given number using a recursive function:
#include <stdio.h> int fibonacci(int i) { if(i == 0) { return 0; } if(i == 1) { return 1; } return fibonacci(i-1) + fibonacci(i-2); } int main() { int i; for (i = 0; i < 7; i++) { printf("%d\t\n", fibonacci(i)); } return 0; }
When the above code is compiled and executed, it produces the following result:
0 1 1 2 3 5 8
SUM OF NATURAL NUMBERS
The following example calculates the sum of natural numbers using a recursive function:
#include <stdio.h> int sum(int n); int main() { int number, result; number = 5; result = sum(number); printf("Sum of %d Natural Numbers Using Recursion is %d\n",number, result); } int sum(int num) { if (num != 0) return num + sum(num - 1); // sum() function calls itself else return num; }
When the above code is compiled and executed, it produces the following result:
Sum of 5 Natural Numbers Using Recursion is 15
Discussions