Download recursion-notes-unit-i

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Dirac delta function wikipedia , lookup

Function (mathematics) wikipedia , lookup

Non-standard calculus wikipedia , lookup

History of the function concept wikipedia , lookup

Computability theory wikipedia , lookup

Halting problem wikipedia , lookup

Factorial wikipedia , lookup

Transcript
Unit – I - Introduction
Topic: Recursion
In general, Recursion is a method of defining a function in terms of its own
definition. In programming, recursion is a call to the same method from a method.
Definition: A recursive function is defined as a function that calls itself to solve a
smaller version of its task until a final call is made which does not require a call to itself.
Every recursive function has two major cases, they are,


Base Case: The problem is simple and can be solved directly without making
further calls to same function.
Recursive Case :
i) Given problem is divided in to simpler sub parts
ii) Function calls itself with subparts of the problem obtained in first step.
iii) Result is obtained by combining solutions of simpler subparts.
Base case acts as the terminating condition. If it is not defined, then recursive
function will generate infinite sequence of calls and will lead to error conditions.
Example: Factorial of a given number is defined as follows.
For any integer n,
n! = n * (n-1)! (Recursive case) and also
1! = 1 (Base Case)
Therefore 5! = 5 *4!; 4! = 4*3!; 3! = 3 * 2!; 2! = 2 * 1!; 1! = 1
Advantages
1.
2.
3.
4.
Recursive solution is shorter and simpler than non-recursive ones.
It is a method to solve problems by solving easier instance of the same problem
Code is clearer and easier to use.
It follows divide and conquer strategy to solve problems.
Disadvantages
1. Sometimes, recursive solutions takes more memory and time to execute
2. Difficult to find bugs if global variables are used in recursive functions
Practice Programs
1. Write a C program to find factorial of a number using recursion.
2.
3.
4.
5.
Write a C program to find the GCD of given two numbers using recursion.
Write a C program to find the nth Fibonacci number using recusion.
Write a C program to generate n numbers in Fibonacci series.
Write a C program to find xy using recursion.
Sample Program – Factorial of a given number
#include<stdio.h>
int fact(int); // function prototype
void main()
{
int num;
clrscr();
printf(“Enter any positive integer : “);
scanf(“%d”,&num);
printf(“The result is : %d“, fact(num));
getch();
}
int fact(int n) // function definition
{
if (n==1)
retrun 1;
else
return n * fact(n-1);
}
Additional Topic – Assignment Questions



1. Types of Recursion - Direct, Indirect, Tail, Linear and Tree with examples
Links :
http://www.dreamincode.net/forums/topic/51296-types-of-recursion/
https://www.tutorialspoint.com/cprogramming/c_recursion.htm
http://www.cquestions.com/2008/01/c-program-to-find-gcd-of-number-using.html
2. Solution for Towers of Hanoi problem