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.
Fibonacci SeriesA series of numbers in which each number is the sum of the two preceding numbers is known as a Fibonacci Series.
General Formula to find the Nth term of a Series
F0 = 0 and F1 = 1 be the seed values
The Nth term of a Fibonacci series is given as Fn = Fn-1 + Fn-2 .
Example,
Input : 5
Fibonacci Series : 0 1 1 2 3
Explanation:
For 3rd term it's 2nd term + 1st term.
For 4th term it's 3rd term + 2nd term.
for 5th term it's 4th term + 3rd term.
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 + " , ");
}
}
}
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;
}
}
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);
} }
public class problem {
static int a = 0, b = 1, nextnum;
public static void main(String[] args) {
int num = 20;
System.out.print(a + “,” + b + “,”);
fib(num);
}
static int fib(int num) {
if (num – 2 > 0) {
int nextnum = a + b;
a = b;
b = nextnum;
System.out.print(“,” + nextnum);
fib(num – 1);
}
return 0;
}
}
import java.util.*;
public class fibbonacci_series {
public static void main(String args[])
{
int a = 0;
int b = 1;
int value = 5;
int store;
System.out.print(a + ” ” + b + ” “);
for(int i = 2 ; i < value ; i++)
{
store = a + b;
a = b;
b = store;
System.out.print(store+" ");
}
}
}
public class custome_Stack {
public static void main(String[] args){
int a=5;
fib(a);
}
static void fib(int n){
int a=0;
int b=1;
int c;
for (int i = 0; i <n ; i++) {
System.out.print(a+" ");
c=a+b;
a=b;
b=c;
}
}
}
public class Fibonacci {
public static void main(String[] args) {
int n1=0, n2=1, n3;
System.out.println(n1+”add”+n2);
for (int i=0; i<7; i++){
n3=n1+n2;
System.out.println(""+n3);
n1=n2;
n2=n3;
}
System.out.println("print fibonacci no:"+n2);
}
}
public class Fibo
{
public static void main (String [] args)
{
int num = 8;
int a = 0, b = 1, c;
for(int i=0 ;i<=num; i++)
{
c=a+b;
a=b;
b=c;
System.out.println(a);
}
}
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);
} }