import java.util.Scanner;
public class JavaExample {
public static void main(String[] args) {
int count, num1 = 0, num2 = 1;
System.out.println("How may numbers you want in the sequence:");
Scanner scanner = new Scanner(System.in);
count = scanner.nextInt();
scanner.close();
System.out.print("Fibonacci Series of "+count+" numbers:");
int i=1;
while(i<=count)
{
System.out.print(num1+" ");
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
i++;
}
}
}
How may numbers you want in the sequence:
10
Fibonacci Series of 10 numbers:0 1 1 2 3 5 8 13 21 34
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.