This Java program generates the Fibonacci series of a given length entered by the user. The program uses an array to store the sequence of Fibonacci numbers, with each number in the sequence equal to the sum of the two preceding numbers.
Overall, this program is a straightforward way to generate the Fibonacci series of a given length in Java using an array. However, it is worth noting that the Fibonacci sequence grows very quickly, and the numbers in the series can become very large for large values of n. In some cases, it may be necessary to use a more efficient algorithm to compute the Fibonacci sequence, such as memoization or matrix exponentiation.
import java.util.Scanner; public class Fabonacci_Series { public static void main(String[] args) { int n; Scanner input = new Scanner(System.in); System.out.print("Enter the Length of Fibonacci Series : "); n = input.nextInt(); int[] num = new int[n]; num[0] = 0; num[1] = 1; for (int i = 2; i < n; i++) { num[i] = num[i - 1] + num[i - 2];//sum of last two numbers } System.out.print("Fibonacci Series : "); for (int i = 0; i < n; i++) { System.out.print(num[i] + " "); } } }
Enter the Length of Fibonacci Series : 8 Fibonacci Series : 0 1 1 2 3 5 8 13
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions