Ask your own question, for FREE!
Mathematics 10 Online
OpenStudy (anonymous):

algorithm for fibonacci series actualy its computer+ maths question but the computer section is deadland. i also want logic for its java program code.

OpenStudy (anonymous):

what is your question?

OpenStudy (anonymous):

algorithm for fibonacci O.o

OpenStudy (anonymous):

Do u know fibonacci series? Can u write the first 5 numbers in it? @aajugdar

OpenStudy (anonymous):

to display fibonacci numbers from 1 to umm 10 maybe yes i know what is fibonacci series @SandeepReddy 1 1 2 3 5 8 13 21 34

OpenStudy (anonymous):

so an algorithm is what?

OpenStudy (anonymous):

algorithm is stepwise presentation of hw is it executed

OpenStudy (anonymous):

first number in fibonacci series is 0 and do u know how fibonacci series form? i mean how to get next number of fibonacci series?

OpenStudy (anonymous):

so for those numbers what is the sequence of how they come up with the next numbers?

OpenStudy (anonymous):

sum of last two numbers

OpenStudy (anonymous):

if the first number was zero how would it get anywhere? you would just keep adding zero to itself?

OpenStudy (anonymous):

i want logic to use it in java explained

OpenStudy (anonymous):

yes exactly so first two numbers are 0 and 1 and we get next numbers by adding last two numbers

OpenStudy (anonymous):

wow, i didnt know that!

OpenStudy (anonymous):

First lets assume the length of series is fixed that is n. where n is a variable in java and you can set it whatever number you want

OpenStudy (anonymous):

You can start with 0. You don't have to though. It's a personal preference thing.

OpenStudy (anonymous):

and now we want to have two variables lets say a and b

OpenStudy (anonymous):

a is intially set to 0 and b is intially set to 1

OpenStudy (anonymous):

getting me?

OpenStudy (anonymous):

yes

OpenStudy (anonymous):

@singlesixx i see, thanks

OpenStudy (anonymous):

okay now oming to the code

OpenStudy (anonymous):

now we want another next number in series right? so now we print the first two numbers first a and b.

OpenStudy (anonymous):

now assign the value of b to a , and the the value of b to a+b.

OpenStudy (anonymous):

understood what i said?

OpenStudy (anonymous):

using "for" loop?

OpenStudy (anonymous):

u can use it .

OpenStudy (anonymous):

how?

OpenStudy (anonymous):

other wise lets take another variable c for the next number in fibonacci series.

OpenStudy (anonymous):

then, intially a is set to 0 , b is set to 1 and c is not initialized,

OpenStudy (anonymous):

its a long method dont you think

OpenStudy (anonymous):

for(int i=1;i<=num;i++){ c= a; a = b; b = a + c; // print c value on screen }

OpenStudy (anonymous):

is it long?

OpenStudy (anonymous):

c= a; a = b; b = a + c;??????????????

OpenStudy (anonymous):

what abt further numbers

OpenStudy (anonymous):

c=a a =b?

OpenStudy (badhi):

If you know recursion, int fib(int num) { if(num==0) return 0; if(num==1) return 1; else { return (fib(num-1)+fib(num-2)); } }

OpenStudy (anonymous):

i want it by for loop >.>

OpenStudy (badhi):

a=0 b=1 for i 1 to n-1 { b=b+a a=b-a } if n==0 then print 0 else print b

OpenStudy (anonymous):

o_o"

OpenStudy (badhi):

well, at first we assign 0 to a and 1 to b. then let s say that we want the 3rd term (n=3). Let's see how the variables are assigned. first iteration - i=1 b=b+a :- b=1+0=1 a=b-a :- a=1-0=1 second iteration - i=2 b=b+a :- b=1+1=2 a=b-a :- a=2-1 = 1 loop will terminate and since n is not zero, it will print the value of b - 2

OpenStudy (anonymous):

thats not how for loop is used

OpenStudy (badhi):

int a=0; int b=1; for (int i=1;i<n;i++) { b+=a; a=b-a; } if (n==0) return 0; else return b; try this on your compiler and verify

OpenStudy (anonymous):

lets see

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!