## Tribonacci Series in Java Program

##

Let's put the logic into a __Java__ program.

```
import java.util.*;
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 x = 1, y = 0, z = 1;
int w = x + y + z;
System.out.println("Tribonacci Series: ");
System.out.print(x+y+z);
for(int i=4; i<=n; i++)
{
System.out.print(+w);
x=y;
y=z;
z=w;
w=x+y+z;
}
System.out.println();
}
}
```

**Output:**

```
Enter Number of Terms: 12
Tribonacci Series:
0, 1, 1, 2, 4, 7, 13, 24, 44, 81, 149, 274
```

**Explanation:- **The initial terms in this case have been initialized with 1,0 and 1 as x, y, and z, respectively. The loop then runs. When we get to the fourth term, it is declared as 'w,' the previous term is printed, and all the terms are swapped with the next elements so that the last three integers can be added and their sum is printed each time. This works until 'n' terms are reached and the entire series is generated.

**Time Complexity**

The time complexity of F(n) can be calculated by counting the number of times its most expensive operation will be executed for n inputs. The operation with the highest runtime cost in this algorithm is addition.

**In the above Example the Time complexity and Space Complexity are:**

**Time Complexity**: O(N)

Because we are only iterating each element once from N = 4 to N = N, the time complexity will be O (N)

**Auxiliary Space:** O(1)

Because we are simply utilising a variable to hold the response, the space complexity of the preceding code is O(1).

## Find N^{th} Term of the Tribonacci Series

```
import java.util.*;
public class nthTribonacciTerm
{
public static int tribonacciTerm(int n)
{
if (n < 3)
return n;
int x = 0, y = 1, z = 1, w;
while (n-- > 3)
{
w = x + y + z;
x = y;
y = z;
z = w;
}
return z;
}
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the nth term: ");
int n=scan.nextInt();
int answer =tribonacciTerm(nth);
System.out.println("The "+n+"th term of the Tribonacci series is: "+answer);
}
}
```

**Output:**

```
Enter the nth term: 20
The 20th term of the Tribonacci series is: 35890
```

Practice it on online java compiler for better understanding.

**Explanation:** In the while loop --> is not an operator. These are two separate operators, -- and >. The conditional code decrements n by 1 while returning the original number, not the decremented value, and then uses the > operator to compare the original value to 3. We may express it more precisely as while ((n—) > 3). Rest it is the same as the Tribonacci Series in Java.

**In the above Example the Time complexity and Space Complexity are:**

**Time Complexity**: O(N)

Because we are iterating till the N-th tribonacci number, the time complexity of the following code is O(n). Here, n is the supplied integer for which the N-th tribonacci number must be calculated.

**Auxiliary Space:** O(1)

Because we are simply utilising a variable to hold the response, the space complexity of the preceding code is O(1).

Must Read** **__Type Conversion in Java__

## Frequently Asked Questions

**What Exactly is the Tribonacci Series in Java?**

A Tribonacci sequence or series is an integer sequence in which each fourth term is the sum of the previous three terms.

**Which series is related to the Tribonacci series in Java?**

The Tribonacci series is related to the Fibonacci series.

**Write General form of Tribonacci series in Java?**

General form of the Tribonacci sequence is:

**a(nth) = a(n-1)th + a(n-2)th + a(n-3)th **

Here,

**a(0) =0, a(1) = 1, a(2) = 1**

**What are the first ten terms of the Tribonacci Series in Java?**

Tribonacci Series for the first 10 terms are:

**0, 1, 1, 2, 4, 7, 13, 24, 44, 81**

**What is the 10th term of the Tribonacci Series in Java?**

The 10th term of the Tribonacci series is: **81**

## Conclusion

In this article, we learned about the Tribonacci Series in Java, which includes The Sequence and the Nth term in the Tribonacci Series in Java.

After reading about the Tribonacci Series in Java, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see Fibonacci series in Java, GroupLayout in Java, and BorderLayout in Java.

Recommended Readings:

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more!

Do upvote our blogs if you find them helpful and engaging!