Tribonacci Series - Coder's point

Saturday, March 16, 2019

Tribonacci Series

 //The tribonacci series is a generalization of the Fibonacci sequence where each term is the sum of the three preceding terms.  
 import java.util.*;  
 public class Tribonacci_series  
 {  
   void tribonacci(int n)  
   {  
     int f,s,t;  
     f=0; s=1;t=2;  
     int fourth;int i;  
     System.out.println("\n Tribonacci series---");  
     System.out.print(f+" "+s+" "+t);  
     for( i=3;i<n;i++)  
     {  
       fourth=f+s+t;  
       System.out.print(" "+fourth);  
       f=s;  
       s=t;  
       t=fourth;  
     }  
   }  
   public static void main()  
   {  
     Tribonacci_series ob = new Tribonacci_series();  
     Scanner in = new Scanner(System.in);  
     System.out.println("Enter the limit");  
     int n=in.nextInt();  
     ob.tribonacci(n);  
   }  
 }  
OUTPUT:
Enter the limit
10

 Tribonacci series---
0 1 2 3 6 11 20 37 68 125

No comments:

Post a Comment