you're on the right track.
In the original code:
System.out.println(lo);
while (hi < 50) {
System.out.println(hi); //*** output
hi = lo + hi; // new hi
So you are outputting the hi variable
In your new code:
lo = hi - lo;
array[i++] = hi; // *** adding to array
array[i++] = lo; // *** adding to array
}
so you are 'outputting' both the hi and lo variables
hth
mcalex
On Oct 23, 9:54 pm, Sadia Butt <[email protected]> wrote:
> Hi all
>
> I am trying to make a program. I saw the program in a book to generate
> fibonacci series. Now they have something this
>
> code:
>
> class Fibonacci {
> /** Print out the Fibonacci sequence for values < 50 */
> public static void main(String[] args) {
> int lo = 1;
>
> int hi = 1;
>
> System.out.println(lo);
> while (hi < 50) {
> System.out.println(hi);
> hi = lo + hi; // new hi
> lo = hi - lo; /* new lo is (sum - old lo)
> that is, the old hi */
> }
> }}
>
> Now I understood this but now the exercises in the book is that I have to do
> backward sequence one & 2nd to save the sequence in an array then display it.
> I am now focusing in saving the values in an array, but I am unable to solve
> it.
>
> this is what I modified.
>
> int lo = 1;
>
> int hi = 1;
> int array[]= new int [9] ;
> int i = 0;
> // System.out.println(lo);
>
> while (hi < 50) {
> // System.out.println(hi);
> hi = lo + hi;
> lo = hi - lo;
> array[i++] = hi;
>
> array[i++] = lo;
>
> }
> for (int k = 0; k < array.length; k++)
> System.out.println(array[k]);
> }
>
> but when I do this it goes array index out of bonds. which is understood I am
> adding i more then the length of array. so how can I generate the series & at
> the same time save it in an array in the loop.??
>
> sorry for the long question
>
> Sadia Butt
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---