Java program to generate the fibonacci series using iteration
ADVERTISEMENTS
Java program to generate the Fibonacci series using iteration, there are you will learn how to make a Java program to find the Fibonacci series of any number of terms.
You have to knowledge of the Fibonacci series concepts to complete out this before made this program.
Formula to find the Fibonacci series of any number of terms
The Fibonacci series is: 1, 3, 4, 7, 11, 18, 29, 47
Let us understand this through a java program:
// Java program to generate the Fibonacci series using iteration
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of terms to generate the Fibonacci series::\n");
int x = in.nextInt(),
i,
t1 = 1,
t2 = 3,
nt;
// x denotes number of terms
// t1 denotes term 1
// t2 denotes term 2
// nt denotes next term
System.out.print("Fibonacci Series:: ");
for (i = 1; i <= x; ++i) {
System.out.print(t1 + ", ");
nt = t1 + t2;
t1 = t2;
t2 = nt;
}
System.out.println("\n");
}
}
Output:
Enter the number of terms to generate the Fibonacci series::
8
Fibonacci Series:: 1, 3, 4, 7, 11, 18, 29, 47,
Tags:
Fibonacci series using recursion in java
Fibonacci series in java using scanner
Fibonacci series in java using for loop
Fibonacci series in java program