Opening Hours :7AM to 9PM
public class Fibonacci { public static void main(String[] args) { int n = 10; // number of elements in the series int first = 0; int second = 1; System.out.print(first + " " + second); // printing first two elements for (int i = 2; i < n; i++) { int next = first + second; System.out.print(" " + next); first = second; second = next; } } }This program will generate the first 10 numbers in the Fibonacci series: 0 1 1 2 3 5 8 13 21 34. You can change the value of n to generate a different number of elements in the series. You can also use recursion to generate the Fibonacci series in Java. Here is an example:
public class Fibonacci { public static int fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } else { return fibonacci(n - 1) + fibonacci(n - 2); } } public static void main(String[] args) { int n = 10; // number of elements in the series for (int i = 0; i < n; i++) { System.out.print(fibonacci(i) + " "); } } }In this example, the fibonacci() method is a recursive function that takes an integer n as input and returns the nth element in the Fibonacci series. The main() method uses this function to generate the first 10 numbers in the series and prints them to the console. Both of these examples generate the Fibonacci series using different approaches, but they produce the same result. You can use whichever method you prefer in your own program.