Fibonacci series program in java using multithreading
A Fibonacci series is a set of numbers, typically beginning with 0 and 1, where each number is the sum of the two before it. In Java, it is possible to create a program that generates the Fibonacci series using multithreading. In Java, the Fibonacci series can be generated using a for loop and a few variables to store the previous numbers in the sequence.Here is an example of how to generate the Fibonacci series in Java up to a certain number of terms:
Program:
public class Fibonacci {
public static void main(String[] args) {
int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
for (int i = 1; i <= n; ++i)
{
System.out.print(t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
Output:
First 8 terms:
0, 1, 1, 2, 3, 5, 8, 13
In this example, the variable "n" is used to specify the number of terms to generate in the Fibonacci series, and the variables "t1" and "t2" are used to store the previous numbers in the sequence. The for loop iterates from 1 to "n," printing the value of "t1" after each iteration and then updating "t1" and "t2" values to reflect the sum of the previous values. By doing this, the Fibonacci series is generated up to the desired number of terms . You can also create Fibonacci series using recursive function as well, here is an example of how to generate Fibonacci series using recursive function
Program:
public class Fibonacci {
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
printFibonacci(count-2);//n-2 because 2 numbers are already printed
}
}
Output:
0 1 1 2 3 5 8 13 21 34
In this example, the function printFibonacci is called with count as an argument, where count is the number of terms you want to generate in the series. The function uses recursion to generate the Fibonacci series. The base case for the recursion is when the count is less than or equal to 0, in which case the function terminates. Each call to the function generates the next number in the series, and then makes a recursive call with a decremented count.It's worth noting that in both examples, the Fibonacci series starts with 0 and 1, but you can easily change that by adjusting the initial values of t1 and t2, or n1 and n2. The examples shown above can be used as a starting point for generating the Fibonacci series in your own Java programs.Here is an example of such a program:
Program:
import java.util.Scanner;
public class FibonacciThread extends Thread {
int n;
int[] series;
public FibonacciThread(int n) {
this.n = n;
series = new int[n];
}
public void run() {
series[0] = 0;
series[1] = 1;
for (int i = 2; i < n; i++) {
series[i] = series[i-1] + series[i-2];
}
} public void printSeries() {
for (int i = 0; i < n; i++) {
System.out.print(series[i] + " ");
}
}public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements in the series: ");
int n = sc.nextInt();
FibonacciThread ft = new FibonacciThread(n);
ft.start();
try {
ft.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
ft.printSeries();
}
}
Output:
Enter the number of elements in the series: 8
0 1 1 2 3 5 8 13
In this program, a class called FibonacciThread is defined that extends the Thread class. The function Object() of this class accepts an integer as a parameter, denoting the number of components in the Fibonacci sequence. The constructor initializes an array called series with the size of the input integer.The run() method is overridden from the Thread class, and it is used to generate the Fibonacci series using a for loop. The first two elements of the series are initialized as 0 and 1, respectively, and the next elements are calculated by adding the sum of the two preceding elements.The printSeries() method is used to print the generated series.