Java program to print Tribonacci Series.



A tribonacci series is one in which the sum next term is the sum of previous three terms
Example 0 1 2 3 6 11 20………….


import java.util.Scanner;
public class Tribonacci 
 {
 public static void main(String args[]) {
  func(10);
 }
 static void func(int n) {
  int a = 0, b = 1, c = 2, d = 0, i;
  System.out.print(a + " , " + b + " , " + c);
  for (i = 4; i <= n; i++) {
   d = a + b + c;
   System.out.print(", " + d);
   a = b;
   b = c;
   c = d;
  }
 }
}

Output:

0 , 1 , 2, 3, 6, 11, 20, 37, 68, 125
BUILD SUCCESSFUL (total time: 1 second)


No comments:

Post a Comment