Today in this article we will teach you Java Program To Print Tribonacci Series without using arrays, Tribonacci series is quite similar to the Fibonacci sequence, So if you want to learn about Fibonacci Series you must check our recent article about the Fibonacci series.
Java Program To Print Tribonacci Series
- Declare and initialize 4 Integer type variables eg: int num1 = 0, int num2 = 1, int num3 = 2, int numR = 0.
- Declare another variable of integer type: int N;
- print a statement like (“Print Tribonacci Series Upto N numbers: “);
- initialize the variable N using the Scanner class
- print(num1+” “+num2+” “+num3);
- use For loop and print the Tribonacci Series up to N number: for(int i = 3; i < N; i++)
- Store the sum of the first 3 terms into numR : numR = num1+num2+num3;
- print the value of numR
- assign the value of num2 to num1
- assign the value of num 3 to num2
- assign the value of numR to num3
Below is the implementation of the above approach
import java.util.Scanner; public class TribonacciSeries { public static void main(String[] args) { int num1 = 0; int num2 = 1; int num3 = 2; int numR = 0; int N; System.out.println("Print Tribonacci Series Upto N numbers : "); Scanner sc = new Scanner(System.in); N = sc.nextInt(); System.out.print(num1+" "+num2+" "+num3); for(int i = 3 ; i < N ; i++){ numR = num1+num2+num3; System.out.print(" "+numR); num1 = num2; num2 = num3; num3 = numR; } } }
Output :
0,1, 2, 3, 6 series in java :
The Tribonacci series is basically a general form of the Fibonacci series.
Tribonacci series nth term = Term(n) = (n-1) + (n-2) + (n-3)