+ 3
How to find fibonacci series till 13 using for loop?
10 Antworten
+ 2
8th number is 13.
https://code.sololearn.com/c7F8je1ovc2S/?ref=app
+ 8
@Akshat Nothing, its an escape character for an indentation tab, you could remove it.
+ 7
int a = 1, b = 1;
for (int i = 1; i < 8; i++) {
System.out.print (a+"\t");
int res = a+b;
a = b;
b = res;
}
+ 6
The Fibonacci sequence itself is built upon the concept of recursion.
+ 3
thanks👍
+ 3
So what is Fibonacci's numbers?
1, 1, 2, 3, 5, 8, 13
Let's start simple:
0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5
3 + 5 = 8
5 + 8 = 13
A + B = C
Now if you look carefully you can see that in this sequence Fibonacci is repeated:
- At A it starts 0, 1, 1, 2...
- At B it starts 1, 1, 2, 3....
- At C it starts 1, 2, 3, 5....
With this we see that we want B's values.
int a = 0;
int b = 1;
int c = a + b;
We want a and b to change the whole time so we put it in a for loop:
int a = 0;
int b = 1;
int c = 1;
while ( b <= 13){ // While B is smaller-equal to 13
System.out.print(b);
a = b;
c = a + b;
b = c;
}
Then we move everything up.
0 + 1 = 1;
a + b = c;
then a = 1 because b is 1, b = 1 because c is 1, c = 2 because a + b is 2
+ 2
what's the use of taking '/t' here?
+ 2
Here is something that can maybe help you
https://code.sololearn.com/cCT1D9J4T9zQ/?ref=app
+ 2
can you just explain a little about that.😐