# Fibonacci series up to n using Java ## Find the Fibonacci Series up to Nth Term in Java

For a Given integer input number as the Nth value, the objective is to Find the Fibonacci Series up to the Nth Term. Therefore, we’ll write a program to Find the Fibonacci Series up to Nth Term in Java Language.

```Example
Input : 4
Output : 0 1 1 2
```

## Find the Fibonacci Series up to Nth Term in Java Language

Given an integer input for the Nth value, the objective is to Find the Fibonacci Series up to the Nth term using Loops and recursion in Java Language. To do so we’ll use the following methods,

• Method 1: Using Iteration
• Method 2: Using Recursion
• Method 3: Using Formula

We’ll discuss the above mentioned methods in detail in the upcoming sections. For better understanding, checkout the section below.

### Java Code

Run
```public class Main
{
public static void main (String[]args)
{

int num = 15;
int a = 0, b = 1;

// Here we are printing 0th and 1st terms
System.out.print (a + " , " + b + " , ");

int nextTerm;

// printing the rest of the terms here
for (int i = 2; i < num; i++)
{
nextTerm = a + b;
a = b;
b = nextTerm;
System.out.print (nextTerm + " , ");
}

}
}```

### Output

`0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377`

### Java Code

Run
```Run
public class Main
{
static int a = 0, b = 1, nextTerm;
public static void main (String[]args)
{

int n = 15;
// Here we are printing 0th and 1st terms
System.out.print (a + " , " + b + " , ");

// n-2 as 2 terms already printed
Fib (n - 2);
}

static int Fib (int n)
{
if (n > 0)
{
nextTerm = a + b;
a = b;
b = nextTerm;

System.out.print (nextTerm + " , ");
Fib (n - 1);
}
return 0;

}

}
```

### Output

`0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377`

### Java Code

Run
```public class Main
{
static int a = 0, b = 1, nextTerm;
public static void main (String[]args)
{

int n = 15;

fib (n);
}

static void fib (int n)
{

int f[] = new int[n + 1];

f = 0;
f = 1;

System.out.print (f + " , " + f + " , ");

for (int i = 2; i < n; i++)
{
f[i] = f[i - 1] + f[i - 2];
System.out.print (f[i] + " , ");
}
}

}

```

### Output

`0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377`

### Related Banners

Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription

### One comment on “Fibonacci series up to n using Java”

• Satendra

class hallo{
public static void fb(int a, int b, int n){
if(n==0){
return;
}
int c=a+b;
System.out .println(c);
fb( b,c, n-1);
}
public static void main (String[] args){
int n=8,a=0,b=1;
System.out.println(a);
System.out.println(b);
fb( a, b, n-2);
} } 10