nth term Tn of The Tribonacci sequence is defined as follows:
T0(0th term) = 0, T1(1st term) = 1, T2(2nd term) = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0.
Take n as an integer inout, print the value of Tn(nth term) as an integer output.
Input Format
For each test case, n will be given as an integer input.
Constraints
0<=n<=37
Output Format
Print the value of nth Tribonacci number.
Sample Input 0
0
Sample Output 0
0
Sample Input 1
1
Sample Output 1
1
Sample Input 2
2
Sample Output 2
1
Sample Input 3
7
Sample Output 3
24
Sample Input 4
10
Sample Output 4
149
Sample Input 5
11
Sample Output 5
274
Sample Input 6
20
Sample Output 6
66012
ALSO READ: Program to Transform a number
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a=1, b=1, c=2;
if(n==0)
System.out.print(0);
else if(n==1)
System.out.print(a);
else if(n==2)
System.out.print(b);
else if(n==3)
System.out.print(c);
else{
int d=-1;
for(int i=4; i<=n; i++){
d=a+b+c;
a=b;
b=c;
c=d;
}
System.out.print(d);
}
}
}