Recursive Functions
Recursive Functions
Recursive Functions
A:INITIALIZATION: 0!= 1
RECURSION: n != n · (n -1)!
Compute 5!
L16 6
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
f(5)=
5·f(4)
L16 7
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
f(4)= f(5)=
4·f(3) 5·f(4)
L16 8
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 9
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 10
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 11
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 12
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 13
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 14
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
L16 15
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
4·6= f(5)=
24 5·f(4)
L16 16
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
5·24=
120
L16 17
Recursive Algorithms Computer
Implementation
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
Return 5!
= 120
L16 18
public void recursion(int y)
{
if(y>0)
{
System.out.println(y);
recursion(y-1);
System.out.println(y);
}
}
Sample Recursive Function
int func ( int n)
{
if ( n= = 1 )
return( 1 );
else
return( n + func ( n - 1)) ;
}
http://www.programmerinterview.com/index.php/recursion/explanati
on-of-recursion/
http://www.cs.wustl.edu/~kjg/cse131/modules/recursion/lab.html
Function Stack –
https://www.youtube.com/watch?v=jRcll9qY6b0
Recursion
https://www.youtube.com/watch?v=PORo1ut9kMs
Exercise
}
void display()throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter any word :");
String s=in.readLine();
func(s,s.length( )-1);
}
import java.io.*;
class recursive1
{static int i=0;String c="";
String rev(String s)
{
if (i<s.length())
{
char ch=s.charAt(i);i++;
rev(s);
c=c+ch;
return c ;
}
else return "";
}
void display()throws IOException
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter any word :");
String s=in.readLine();
System.out.println("Reverse of the word = "+rev(s));
}}
class funct
{
static int i=2;
public static int fun(int num)
{
if(num%i==0 && num!=i)
return 0;
if(i<num)
{
i++;
return fun(num);
}
else
return 1;
}
}
public static boolean isPrime(int num)
{ int result;
result =Prime(num, num-1);
if(result>1)
return false;
else
return true;
}
public static int Prime(int number, int i)
{
int prime= number%i;
if (prime >0 && i>1)
{
return Prime(number, i - 1);
}
else
{
return i;
}