What does your code do:
arr(3) = 0 + 1 arr(4) = 0 + 1 arr(5) = 0 + 1
etc .. You assign the same value over and over.
You need to do:
arr(i) = arr(i - 1) + arr(i - 2);
Explanation:
Let's say you have an array:
and i is in index 3, this assigns arr(3) = arr(2) + arr(1) equal to 1 + 0. Therefore, arr (3) = 1
i now at index 4, this will assign arr(4) = arr(3) + arr(2) , which is 1 + 1. Therefore, arr (4) = 2
source share