DSA With C Recursion
DSA With C Recursion
1
What is Recursion?
Recursion is the process of repeating items in a self-similar way.
In programming languages, if a program allows you to call a function
inside the same function, then it is called a recursive call of the function.
Any function which calls itself is called recursive function and such
function calls are called recursive calls.
Recursion cannot be applied to all problems, but it is more useful for the
tasks that can be defined in terms of a similar subtask.
It is idea of representing problem a with smaller problems.
When recursive function call itself, the memory for called function
allocated and different copy of the local variable is created for each
function call.
But while using recursion, programmers need to be careful to define an
exit condition from the function, otherwise it will go into an infinite loop.
What is Recursion?
Some of the problem best suitable for recursion are
• Factorial
• Fibonacci
• Tower of Hanoi
Working of Recursive function
void func1();
void main()
{
....
func1();
.... Function
} call
void func1()
{ Recursive
.... function call
func1();
....
}
Recursion - factorial example
The factorial of a integer n, is product of
• n * (n-1) * (n-2) * …. * 1
Recursive definition of factorial Recursive trace
• n! = n * (n-1)!
Final Ans 5 *24 = 120
• Example
• 3! = 3 * 2 * 1 Fact(5)
• 3! = 3 * (2 * 1) return 4 * 6 = 24
call
• 3! = 3 * (2!)
Fact(4)
return 3 * 2 = 6
call
Fact(3)
return 2 * 1 = 2
call
Fact(2)
return 1
call
Fact(1)
WAP to find factorial of given number using Recursion
Program
#include <stdio.h>
int fact(int);
Output
void main()
{ Enter the number? 5
int n, f; factorial = 120
printf("Enter the number?\n");
scanf("%d", &n);
f = fact(n);
printf("factorial = %d", f);
}
int fact(int n)
{
if (n == 0)
return 1; Example::
else if (n == 1)
return 1; fact_recursion.c
else
return n * fact(n - 1);
}
Recursion - Fibonacci example
A series of numbers, where next
number is found by adding the
two number before it.
Recursive definition of Fibonacci Recursive trace
• Fib(0) = 0 Final Ans. 3
Fib(4)
• Fib(1) = 1
return 1
• Fib(n) = Fib(n-1) + Fib(n-2) return 2
return 1 return 0
Fib(1) Fib(0)
WAP to Display Fibonacci Sequence(Example::
fibo_recursion.c)
Program
#include <stdio.h> Program contd.
int fibonacci(int); int fibonacci(int n)
void main() {
{ if (n == 0 || n == 1)
int n, m = 0, i; return n;
printf("Enter Total terms\n"); else
scanf("%d", &n); return (fibonacci(n
printf("Fibonacci series\n"); - 1) + fibonacci(n - 2));
for (i = 1; i <= n; i++) }
{
printf("%d ", fibonacci(m));
m++; Output
} Enter Total terms
} 5
Fibonacci series
0 1 1 2 3
Recursion - Decimal to Binary example
To convert decimal to binary, divide decimal number by 2 till
dividend will be less then 2
To convert decimal 13 to binary
• 13/2 = 6 reminder 1
• 6/2 = 6 reminder 0
• 3/2 = 3 reminder 1
• 1/2 = 1 reminder 1
Recursive definition of Decimal to Binary
• decToBin(0) = 0
• decToBin(n) = n%2 + 10* decToBin(n/2)
Example
• decToBin(13) = 13%2 + 10 decToBin(6)
• decToBin(13) = 1101
Recursion - Decimal to Binary example
Recursive trace
decToBin(13)
return 6%2 + 10*11 = 110
call
decToBin(6)
return 3%2 + 10*1 = 11
call
decToBin(3)
return 1%2 + 10*0 = 1
call
decToBin(1)
return 0
call
decToBin(0)
WAP to Convert Decimal to Binary
#include <stdio.h>
Output
int convertDecimalToBinary(int);
void main() Enter a decimal numbe
{ r: 12
int dec, bin; The binary equivalent
printf("Enter a decimal number: "); = 1100
scanf("%d", &dec);
bin = convertDecimalToBinary(dec);
printf("The binary equivalent = %d \n",bin);
}
int convertDecimalToBinary(int dec)
{
if (dec == 0)
return 0;
else
return (dec % 2 + 10 *
convertDecimalToBinary(dec / 2));
}