Making a very slight change to your code, use the length()
function check the length (number of elements) of fib
before each iteration of the while loop.
while(length(fib)<100){....}
This way you avoid creating a further variable to count the number of iterations, as in the other answers.
For what it’s worth, this is how I would do it – a for loop is more appropriate as you have a set number of iterations
n <- 100
fib <- c(0,1)
for(i in 3:n){
fib[i] <- fib[i-2] + fib[i-1]
}
CLICK HERE to find out more related problems solutions.