# Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers.

## Tetranacci number

Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends upon the final four Tetranacci numbers, but a Fibonacci number depends exclusively on the final two Fibonacci numbers.

The Tetranacci numbers are shown as follows in mathematics:

T(0) = 0,

T(1) =1,

T(2) =1,

T(3) =2,

T(n) = T(n - 4) + T(n - 3) + T(n - 2) + T(n - 1), where n >= 4

hence

T(4) = T(0) + T(1) + T(2) + T(3) = 0 + 1 + 1 + 2 = 4

T(5) = T(1) + T(2) + T(3) + T(4) = 1 + 1 + 2 + 4 = 8

T(6) = T(2) + T(3) + T(4) + T(5) = 1 + 2 + 4 + 8 = 15

### Java Program for Tetranacci number :

The program below will describe the tetranacci number using mathematical formulas.

Example:TetranacciExample.java

``````// this program is for finding the first 10 tetranacci numbers
import java.io.*;
import java.util.*;
public class TetranacciExample
{
// this method for finding the n tetranacci numbers
int findTetranacci(int num)
{
// the array of the size 4 is declared
// the next tetranacci number will depend upon the
// the 4 numbers
int array[] = new int;
// the array was initialized
// the first four numbers of the array
// the mathematical formula is declared
array = 0;
array = 1;
array = 1;
array = 2;
if(num <= 3)
{
return array[num];
}
// loop for finding the tetranacci numbers
for(int i = 4; i <= num; i++)
{
// The remaining 4 components determine the present Tetranacci number.
int tem = array + array + array + array;
array = array;
array = array;
array = array;
array = tem;
}
// array will consist of the tetranacci number
return array;
}
// main section
public static void main(String argvs[])
{
// an object is created for the class TetranacciExample
TetranacciExample object = new TetranacciExample();
System.out.println("The initial 10 Tetranacci numbers are: ");
// loop for finding the tetranacci numbers for the given range
for(int i = 0; i <= 9; i++)
{
int nof = object.findTetranacci(i);
System.out.print(nof + " ");
}
}
}  ``````

Output

## Tetranacci number: Recursive Approach

The following mathematical method also functions as a recursive formula to determine the additional Tetranacci numbers if we consider the values of the first four Tetranacci numbers to represent the base case. The next Tetranacci number is generated recursively using the following format above method.

Example: TetranacciRecursive.java

``````// This program is for finding the first 10 tetranacci numbers
//by using the recursive approach
//package section
Import java.io.*;
Import java.util.*;
public class TetranacciRecursive
{
//  this method for finding the n tetranacci numbers
int findTetranacci(int num)
{
// the below mentioned 3 if conditions are for the
// for finding the first four tetranacci numbers
// which has the base values
if(num <= 1)
{
return num;
}
if(num == 2)
{
return 1;
}
if(num == 3)
{
return 2;
}
// for finding the next Tetranacci number, the previous 4 numbers are very mandatory
// this method is for the recursive approach
return findTetranacci(num - 1) +
findTetranacci(num - 2) +
findTetranacci(num - 3) +
findTetranacci(num - 4);
}
//main section of the program
public static void main(String argvs[])
{
// an object is created for the class TetranacciRecursive()
TetranacciRecursive object = new TetranacciRecursive();
System.out.println("The first 10 tetranacci numbers of the series are: ");
// loop for finding the Tetranacci numbers
for(int i = 0; i <= 9; i++)
{
int number = object.findTetranacci(i);
System.out.print(number + " ");
}
}
}  ``````

Output

## Finding the Nth Tetranacci number

The below program will describe finding the Nth Tetranacci number

Example:NthTetranacciNumber.java

``````// This program is for finding the nth tetranacci number
//import section
import java.io.*;
import java.util.*;
public class NthTetranacciNumber
{
// declaring the function for returning the nth tetranacci number
static int displayTetraRec(int num)
{
//the base case for finding the tetranacci number is declared using the if the condition
if (num == 0)
return 0;
// base condition
if (num == 1 || num == 2)
return 1;
// base condition
if (num == 3)
return 2;

else
return displayTetraRec(num- 1) +
displayTetraRec(num- 2) +
displayTetraRec(num- 3) +
displayTetraRec(num- 4);
}

// function for displaying the result of the nth tetranacci number
static void displayTetra(int num)
{
System.out.println(displayTetraRec(num) + " ");
}

// main code of the program
public static void main(String[] args)
{
int num= 15;
displayTetra(num);
}
}``````

Output