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: ";
    cin>>N;

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

    } 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 :)

Move your cout statement to after the for loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.