Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
public class FibonacciSeries {
// Method to print Fibonacci series
public static void printFibonacci(int n) {
int first = 0, second = 1;
System.out.println("Fibonacci Series:");
for (int i = 1; i <= n; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
}
public static void main(String[] args) {
int terms = 10; // Number of terms passed as parameter
printFibonacci(terms);
}
}
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.