java - Fibonacci Numbers Array & another copy of Array whose elements are divisible by 2 (Euler Prob 2), swapping out null elements -


this program finds fibonacci numbers , set of fibonacci numbers divisible 2.

problem code: i'm unable remove 2nd array's elements, namely contain zeroes or null.

for example, following sample output has zeroes not qualify fibinacci nos. divisible 2 (i.e., under call of euler 2 problem):

        fib nos div 2 except 0s:          0 2 0 0 8 0 0 34 0 0 144 0 0 610 0 0 2584 0 0 0          output should be:         2 8 34 144 610 2584  

and code:

import java.util.scanner;   public class fibonacci_arrays {      public static void main(string[] args) {             int limit = 0;             scanner scan = new scanner(system.in);             //number of elements generate in series             system.out.println("enter how many fibonacci numbers generate: " + "\n");              limit = scan.nextint();               long[] series = new long[limit]; //contains of fib nos. limit specified.               //create first 2 series elements             series[0] = 0;             series[1] = 1;              long[] divseries = new long[series.length]; //contains fib nos. divisible 2.              //create fibonacci series , store in array             for(int i=2; < limit; i++){                      series[i] = series[i-1] + series[i-2];                     if ((series[i] % 2) == 0){                         divseries[i] = series[i];                         //need remove 0 entries divseries array.                     }              }              //print fibonacci series numbers             system.out.println("fibonacci series upto " + limit);             for(int i=0; i< limit; i++){                     system.out.print(series[i] + " ");             }              system.out.println();               //print euler problem 2 series numbers             system.out.println("fib nos div 2 except 0s: ");             for(int i=0; i< limit; i++){                     system.out.print(divseries[i] + " ");         }     } } 

it's better declare divseries arraylist store final result because of unknown number of fibonacci number divisible two.

so, code be:

 //contain fibonacci number divisible 2.   list<long> divseries = new arraylist<>();   for(int i=2; < limit; i++){       series[i] = series[i-1] + series[i-2];       if ((series[i] % 2) == 0){                divseries.add(series[i]);         }       }       //to print result   for(long : divseries)         system.out.print(i + " "); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -