In the 13th century, the Italian mathematician Leonardo Fibonacci—as a way to explain the geometric growth of a population of rabbits—devised a mathematical sequence that now bears his name. The first two terms in this sequence, Fib(0) and Fib(1), are 0 and 1, and every subsequent term is the sum of the preceding two. Thus, the first several terms in the Fibonacci sequence look like this:
Please post some more specifics on what it is that you struggle with about this question, or post your code so far and tell us where you are stuck.
A hint to get you started:
Use separate variables to keep the previous two numbers which you update with every loop iteration.
To clarify Mr, Soranz comment:
Forum Rules wrote:
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves. Those questions are for you to work out, so that you will learn from the experience. It is OK to ask for hints, but not for entire solutions.