Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Iterative Constructs in Java

Chapter 9

Iterative Constructs in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Program to Print Tribonacci Series


18. Write a program to read the number n using the Scanner class and print the Tribonacci series: 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81 . . . and so on.

Hint: The Tribonacci series is a generalisation of the Fibonacci sequence where each term is the sum of the three preceding terms.

import java.util.Scanner;

 

public class TribonacciSeries {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

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

        int n = sc.nextInt();

 

        int a = 0, b = 0, c = 1;

        int next;

 

        System.out.print("Tribonacci Series: ");

 

        if (n >= 1)

            System.out.print(a + " ");

        if (n >= 2)

            System.out.print(b + " ");

        if (n >= 3)

            System.out.print(c + " ");

 

        for (int i = 4; i <= n; i++) {

            next = a + b + c;

            System.out.print(next + " ");

 

            a = b;

            b = c;

            c = next;

        }

    }

}

Output

Sample Input
Enter number of terms: 11
Sample Output 
Tribonacci Series: 0 0 1 1 2 4 7 13 24 44 81

📝 Explanation

  • First three terms are 0, 0, 1
  • Each next term is the sum of previous three terms
  • Values are shifted after every iteration

🔑 Logic Used

next = a + b + c

a = b

b = c

c = next