Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

Java program to print Tribonacci Series.

Somdeb Burman | 6:18:00 AM | 4 comments

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)

Java Resources are available for Download from below

Category: , , ,

4 comments:


  1. import java.util.*;
    /**Wap to input the trionacci series of n terms(input by user).
    * ex:- if n=5;
    * A series of numbers in which each number is the sum of the 2 preceding numbers.
    * Then tribonacci series is=0,1,1,2,4,7
    * Means :-0,1,1,2,4,7,13,24,44.....
    */
    class sc80_TribonacciSeries
    {
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of terms = ");/**Here i took the num of terms till which to print the series.*/
    int n = sc.nextInt();/**initialize*/
    int t1 = 0;
    int t2 = 1;
    int t3 = 1;
    int t4 = 0;
    System.out.print(t1 + "," + t2 + "," + t3 + ",");
    for(int i = 3; i <= n; i++)/**The loop is started from 2 coz the first 2 numbers are
    already printed 0,1*/
    {
    t4 = t1 + t2 + t3;/**This is the main term where we find the next series.*/
    System.out.print(t4 + ",");
    t1 = t2;//Here comes the twist
    t2 = t3;
    t3 = t4;//which I have made in the Dry run
    }
    }
    }

    ReplyDelete
  2. 1 import java.util.Scanner;

    2 public class Tribonacci {

    3 public static void main(String[] args) {

    4 Scanner ab = new Scanner(System.in);

    5 System.out.print("Enter number of terms: ");

    6 int terms= ab.nextInt();

    7 int a = 0;
    8 int b = 0;
    9 int c = 1;

    10 int d a+b+c; =

    11 System.out.println("\t\t****TRIBONACCI SERIES****");

    12

    13 System.out.print(a+""+b+""+c);

    14 for (int e = 1; e <= terms; e++)

    15 {

    16 System.out.print(" "+d);

    17 a = b;

    18 b = c;

    19 c = d;

    20

    21 d = a + b + c;

    22 System.out.println();

    23

    24

    25

    }

    ab.close();

    }

    26

    ReplyDelete