FIBONACCI SERIES
What is the Fibonacci Series?
The Fibonacci series starts with two numbers: 0 and 1. Each subsequent number in the series is the sum of the two preceding numbers. Mathematically, it is represented as:
Where:
The sequence begins like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
How to Generate the Fibonacci Series?
To generate the Fibonacci series, you only need to add the last two numbers repeatedly. Here’s a quick example:
- Start with 0 and 1.
- Add → Sequence: 0, 1, 1
- Add → Sequence: 0, 1, 1, 2
- Add → Sequence: 0, 1, 1, 2, 3
And so on!
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int n=sc.nextInt();
int a=0,b=1;
System.out.print(a+" "+b+" ");
for(int i=2;i<n;i++){
int temp=a+b;
a=b;
b=temp;
System.out.print(temp+" ");
}
}
}
No comments:
Post a Comment