algorithm for fibonacci series actualy its computer+ maths question but the computer section is deadland. i also want logic for its java program code.
what is your question?
algorithm for fibonacci O.o
Do u know fibonacci series? Can u write the first 5 numbers in it? @aajugdar
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
so an algorithm is what?
algorithm is stepwise presentation of hw is it executed
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?
so for those numbers what is the sequence of how they come up with the next numbers?
sum of last two numbers
if the first number was zero how would it get anywhere? you would just keep adding zero to itself?
i want logic to use it in java explained
yes exactly so first two numbers are 0 and 1 and we get next numbers by adding last two numbers
wow, i didnt know that!
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
You can start with 0. You don't have to though. It's a personal preference thing.
and now we want to have two variables lets say a and b
a is intially set to 0 and b is intially set to 1
getting me?
yes
@singlesixx i see, thanks
okay now oming to the code
now we want another next number in series right? so now we print the first two numbers first a and b.
now assign the value of b to a , and the the value of b to a+b.
understood what i said?
using "for" loop?
u can use it .
how?
other wise lets take another variable c for the next number in fibonacci series.
then, intially a is set to 0 , b is set to 1 and c is not initialized,
its a long method dont you think
for(int i=1;i<=num;i++){ c= a; a = b; b = a + c; // print c value on screen }
is it long?
c= a; a = b; b = a + c;??????????????
what abt further numbers
c=a a =b?
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)); } }
i want it by for loop >.>
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
o_o"
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
thats not how for loop is used
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
lets see
Join our real-time social learning platform and learn together with your friends!