Created
October 4, 2021 10:55
-
-
Save ishanbagchi/d4c16473c9c485115850bf286f2c453f to your computer and use it in GitHub Desktop.
Storing fibonacci series in arraylist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.ArrayList; | |
public class Fibonacci { | |
public static ArrayList<Integer> series(int range) { | |
ArrayList<Integer> list = new ArrayList<>(); | |
list.add(0); | |
if (range == 1) | |
return list; | |
list.add(1); | |
if (range == 2) | |
return list; | |
for (int i = 2; i < range; i++) | |
list.add(list.get(i - 2) + list.get(i - 1)); | |
return list; | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
try { | |
System.out.print("Enter range : "); | |
int range = sc.nextInt(); | |
ArrayList<Integer> series = new ArrayList<>(); | |
series = series(range); | |
for (int element : series) | |
System.out.print(element + " "); | |
System.out.println(); | |
} finally { | |
sc.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment