Thread: Determining the value of a term in a series

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    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 =)

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine series summation
    By kashya in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 08:00 PM
  2. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Determining order from a list of integers...
    By AngKar in forum C Programming
    Replies: 12
    Last Post: 04-23-2006, 03:53 PM
  5. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 12:39 PM