Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,973 questions

51,917 answers

573 users

How to add fibonacci numbers to a list in Java

1 Answer

0 votes
import java.util.*; 
 
public class MyClass {
    public static List<Integer> fibonacci() { 
        Integer max = 300;
 
        Integer a = 0, b = 1; 
        List<Integer> fib = new ArrayList<Integer>();  
        fib.add(a); 
   
        do {
            fib.add(b); 
            int c = a + b; 
            a = b; 
            b = c; 
        } while (b <= max);
     
        return fib;
 
    } 
    public static void main(String args[]) {

        List<Integer> result = new ArrayList<Integer>();  
         
        result = fibonacci(); 
        
        System.out.println(result); 
    }
}
 
 
/*
run:
 
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
 
*/

 



answered May 24, 2019 by avibootz

Related questions

4 answers 249 views
1 answer 173 views
1 answer 163 views
1 answer 186 views
186 views asked May 24, 2019 by avibootz
1 answer 170 views
...