Recursive Series In Java (1) 1/2+(1+1)/2^2+(1+1+2)/2^3+......n terms.(2) 1!/X^2-2!/X^3+......n terms.
Menu driven recursive program to perform.
(1) 1/2+(1+1)/2^2+(1+1+2)/2^3+......n terms.
(2) 1!/X^2-2!/X^3+......n terms.
class Recursion{
public double s1(int N){
if(N==0)
return 0;
else
return(fibo(N)/Math.pow(2,N)+s1(N-1));
}//s1
public double s2(int N,int x){
if(N==0)
return 0;
else if(N%2==1)
return((fact(N)/Math.pow(x,N+1))+s2(N-1,x));
else
return((fact(N)/Math.pow(x,N+1))+s2(N-1,x));
}//s2
public static int fact(int n){
if(n==0)
return 1;
else
return n*fact(n-1);
}//fact
public static int fibo(int x){
if(x==1)
return 0;
if(x==2)
return 1;
else
return(fibo(x-1)+fibo(x-2));
}//fibo
}//End of class
OUTPUT---
FOR s1 , When Input Is 10
Output is 0.859375
FOR s2 , When Input Is 10 & 10
Output is 0.013195296000000002
(1) 1/2+(1+1)/2^2+(1+1+2)/2^3+......n terms.
(2) 1!/X^2-2!/X^3+......n terms.
class Recursion{
public double s1(int N){
if(N==0)
return 0;
else
return(fibo(N)/Math.pow(2,N)+s1(N-1));
}//s1
public double s2(int N,int x){
if(N==0)
return 0;
else if(N%2==1)
return((fact(N)/Math.pow(x,N+1))+s2(N-1,x));
else
return((fact(N)/Math.pow(x,N+1))+s2(N-1,x));
}//s2
public static int fact(int n){
if(n==0)
return 1;
else
return n*fact(n-1);
}//fact
public static int fibo(int x){
if(x==1)
return 0;
if(x==2)
return 1;
else
return(fibo(x-1)+fibo(x-2));
}//fibo
}//End of class
OUTPUT---
FOR s1 , When Input Is 10
Output is 0.859375
FOR s2 , When Input Is 10 & 10
Output is 0.013195296000000002
Comments
Post a Comment