# Determining the value of a term in a series

• 05-03-2010
jbour12
Determining the value of a term in a series
hey, my teacher asks me to write a code that determines the value of the nth term ( the user will input the value of n ) in a series.
The series is called Fibonacci series, which is 1, 1, 2, 3, 5, 8, 13
(every term is the sum of the previos two terms)
Can anybody show me how can i make the code?
ps: this question is solved using the nested for function =)
• 05-03-2010
MK27
There are two ways to do this, iterative and recursive. This is a very common assignment, usually the purpose is to demonstrate recursion, but the iterative method (for loop) is more efficient, and it sounds like that's what you want.

In fact, it does not require nested for loops, it just requires one loop:
Code:

`for (i=0;i<N;i++)`
where N is the element in the series you want.

It requires < 6 lines of fairly simple code, but I'm not going to just give you the whole thing. Here's another clue, these are the variables you need:
Code:

`int i, a = 1, b = 1, x;`
not including N. i is for the for loop, and notice a and b are seeded with the first two numbers in the series.

Try something, if it doesn't work, post what you tried.