Tuesday, May 21, 2013

Program to print Tribonacci Series using Recursive Technique


/**
 * class tribonacci_ser_recur prints tribonacci series using recursive technique.
 * @author: Nitendra Kumar
 * @version: May 21/2013
 */
import java.util.Scanner;
public class tribonacci_ser_recur
{
   public int trib(int n)
   {
       if(n==1)
         return 0;
         else if(n==2)
         return 1;
         else if(n==3)
         return 1;
       else if(n>3)
         return (trib(n-1)+trib(n-2)+trib(n-3));
         else 
         return -1;
    }
    
    public static void main()
    {
        int res;
        Scanner in=new Scanner(System.in);
        tribonacci_ser_recur ob=new tribonacci_ser_recur();
        System.out.println("Enter the limit");
        int limit=in.nextInt();
        for(int i=1;i<limit;i++)
        {  
        res=ob.trib(i);
          System.out.print(res+" ");
        }
    }
}

Output:


Enter the limit
10
0 1 1 2 4 7 13 24 44


/*---------------Program developed by: Nitendra Kumar---------------*/
//For more details visit http://javawithnitendra.blogspot.in

1 comment:

Ur comments r most welcome...