Literally, I have been asked this question ""The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, … ; the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms – i.e., Fib[n] = Fib[n – 1] + Fib[n – 2]. Using this information, write a C++ program that calculates the nth number in a Fibonacci sequence, where the user enters n in the program interactively. For example, if n = 6, the program should display the value 8."

I have code that works for giving me an answer;

#include <stdio.h>
#include <iostream>

using namespace std;

int main () {

    int N;
    unsigned long long Fib(int N);

    cout<<"Enter your value: ";

    if (N<1) {
        cout<<"Please chose another value";

    } else {
            unsigned long long u = 0, v = 1, t;

            for(int i=2; i<=N; i++)
                t = u + v;
                u = v;
                v = t;

                cout<<t<<"is your answer";

            return v;

However, I am looking to find a way to display only one term...When it gives the answers it displays the entire sequence, when really I want only the Nth value (as in the question above). Any help on how to do it would be great. Thanks :)

3 Years
Discussion Span
Last Post by plenty.groover
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.