0% found this document useful (0 votes)
33 views

Recursion in C

The document discusses recursion in C programming. It provides examples of writing recursive functions to calculate factorials and Fibonacci series. It explains the concepts of base case and recursive calls in recursion. Sample programs for both iterative and recursive approaches are given for factorial and Fibonacci problems.

Uploaded by

RK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Recursion in C

The document discusses recursion in C programming. It provides examples of writing recursive functions to calculate factorials and Fibonacci series. It explains the concepts of base case and recursive calls in recursion. Sample programs for both iterative and recursive approaches are given for factorial and Fibonacci problems.

Uploaded by

RK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Recursion in C

Recursion is the process which comes see the 2 ways to write the factorial
program.
into existence when a function calls a
copy of itself to work on a smaller
problem. Any function which calls itself ● Factorial Program using loop
is called a recursive function, and such ● Factorial Program using
function calls are called recursive calls. recursion
Recursion involves several numbers of
recursive calls. However, it is important Factorial Program using loop
to impose a termination condition of
recursion. Recursion code is shorter #include<stdio.h>
than iterative code however it is int main()
difficult to understand. {
int i,fact=1,number;
Recursion cannot be applied to all the printf("Enter a number: ");
problem, but it is more useful for the scanf("%d",&number);
tasks that can be defined in terms of for(i=1;i<=number;i++){
similar subtasks. For Example, fact=fact*i;
recursion may be applied to sorting, }
searching, and traversal problems. printf("Factorial of %d is:
%d",number,fact);
Generally, iterative solutions are more return 0;
efficient than recursion since function }
call is always overhead. Any problem
that can be solved recursively, can Output:
also be solved iteratively. However, Enter a number: 5
some problems are best suited to be Factorial of 5 is: 120
solved by the recursion, for example,
Fibonacci series, factorial finding, etc. Factorial Program using recursion
in C
Factorial Program in C
1. #include <stdio.h>
Factorial of n is the product of all 2. int fact (int);
positive descending integers. Factorial 3. int main()
of n is denoted by n!. 4. {
5. int n,f;
For example: 6. printf("Enter the number
5! = 5*4*3*2*1 = 120 whose factorial you want to
3! = 3*2*1 = 6 calculate?");
7. scanf("%d",&n);
Here, 5! is pronounced as "5 factorial", 8. f = fact(n);
it is also called "5 bang" or "5 shriek". 9. printf("factorial = %d",f);
10. }
The factorial is normally used in 11. int fact(int n)
Combinations and Permutations 12. {
(mathematics). 13. if (n==0)
14. {
There are many ways to write the 15. return 0;
factorial program in c language. Let's 16. }
17. else if ( n == 1) 1. if (test_for_base)
18. { 2. {
19. return 1; 3. return some_value;
20. } 4. }
21. else 5. else if (test_for_another_base)
22. { 6. {
23. return n*fact(n-1); 7. return some_another_value;
24. } 8. }
25. } 9. else
10. {
Output 11. // Statements;
Enter the number whose factorial you 12. recursive call;
want to calculate?5 13. }
factorial = 120
Fibonacci Series in C
We can understand the above In case of fibonacci series, next
program of the recursive method call number is the sum of previous two
by the figure given below: numbers for example 0, 1, 1, 2, 3, 5, 8,
13, 21 etc. The first two numbers of
fibonacci series are 0 and 1.

There are two ways to write the


fibonacci series program:
● Fibonacci Series without
recursion
● Fibonacci Series using
recursion

Fibonacci Series in C without


recursion
Recursive Function
1. #include<stdio.h>
A recursive function performs the tasks 2. int main()
by dividing it into the subtasks. There 3. {
is a termination condition defined in 4. int n1=0,n2=1,n3,i,number;
the function which is satisfied by some 5. printf("Enter the number of
specific subtask. After this, the elements:");
recursion stops and the final result is 6. scanf("%d",&number);
returned from the function. 7. printf("\n%d
%d",n1,n2);//printing 0 and 1
The case at which the function doesn't 8. for(i=2;i<number;++i)//loop
recur is called the base case whereas starts from 2 because 0 and 1
the instances where the function keeps are already printed
calling itself to perform a subtask, is 9. {
called the recursive case. All the 10. n3=n1+n2;
recursive functions can be written 11. printf(" %d",n3);
using this format. 12. n1=n2;
13. n2=n3;
Pseudocode for writing any recursive 14. }
function is given below. 15. return 0;
16. } 6. printf("Enter the value of n?");
Output: 7. scanf("%d",&n);
8. f = fibonacci(n);
Enter the number of elements:15 9. printf("%d",f);
0 1 1 2 3 5 8 13 21 34 55 89 144 233 10. }
377 11. int fibonacci (int n)
12. {
Fibonacci Series using recursion in 13. if (n==0)
C 14. {
15. return 0;
1. #include<stdio.h> 16. }
2. void printFibonacci(int n){ 17. else if (n == 1)
3. static int n1=0,n2=1,n3; 18. {
4. if(n>0){ 19. return 1;
5. n3 = n1 + n2; 20. }
6. n1 = n2; 21. else
7. n2 = n3; 22. {
8. printf("%d ",n3); 23. return
9. printFibonacci(n-1); fibonacci(n-1)+fibonacci(n-2);
10. } 24. }
11. } 25. }
12. int main(){
13. int n; Output
14. printf("Enter the number of Enter the value of n?
elements: "); 12
15. scanf("%d",&n); 144
16. printf("Fibonacci Series: ");
17. printf("%d %d ",0,1);
18. printFibonacci(n-2);//n-2
because 2 numbers are already
printed
19. return 0;
20. }

Output:

Enter the number of elements:15


0 1 1 2 3 5 8 13 21 34 55 89 144 233
377

Another Example of recursion in C

WAP to find the nth term of the


Fibonacci series.

1. #include<stdio.h>
2. int fibonacci(int);
3. void main ()
4. {
5. int n,f;

You might also like