Get the Nth Term in the Fibonacci Sequence I'm trying out a new editor (CLion) and I decided to try an old well known programming problem. I also decided to try out C99 rather than C89.

This program calculates the nth term of the Fibonacci Sequence where the value of the current term is the sum of the 2 previous terms. The first 4 terms are 0, 1, 1, 2. The upper limit applied in the program is the 91st term, on my computer using long long the term value goes negative at 93rd term.

All Comments and observations are appreciated. I'm especially interested in performance, function and variable names and suggestions about how I can increase the range of terms using integers. I know I can use doubles but that may incur floating point errors at some point. As far as performance goes, the program has a nice small memory signature (0.5% of the system memory) speed is where I would be worried.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

static const int MinimumTerm = 0;
static const int MaximumTerm = 91;
static const int MaxTries = 3;
static const int inputFailure = -1;

static int getTermCount()
{
    char *fmtstr = "Please enter an integer value between %d and %d\n";
    printf(fmtstr, MinimumTerm, MaximumTerm);

    int term = 0;
    int count = 0;
    do
    {
        scanf("%d", &amp;term);
        if (count &lt; MaxTries)
        {
            if (term &lt; MinimumTerm || term &gt; MaximumTerm)
            {
                fprintf(stderr, fmtstr, MinimumTerm, MaximumTerm);
            }
        }
        else
        {
            return inputFailure;
        }
        count++;
    } while (term &lt; MinimumTerm || term &gt; MaximumTerm);

    return term;
}

static long long getNthFibonacciTerm(int term)
{
    int termCount = 2;
    long long prevValue = 0;
    long long nthValue = 1;

    switch  (term)
    {
        case 0: return 0;
        case 1: return 1;
    }

    while  (termCount &lt; term)
    {
        long long newValue = prevValue + nthValue;
        prevValue = nthValue;
        nthValue = newValue;
        termCount++;
    }

    return  nthValue;
}

int main()
{
    int term = getTermCount();
    if (term &lt; MinimumTerm)
    {
        fprintf(stderr, "Invalid Input\n");
        return EXIT_FAILURE;
    }

    printf("The %d term of the Fibonacci Sequence is %lld\n", term, getNthFibonacciTerm(term));

    return EXIT_SUCCESS;
}
A few points about improving code


It seems that your naming convention for constants is to use a capitalized identifier. If so, that should be applied consistently for all constants including inputFailure.
The loop in the function getTermCount can be simplified. A for-loop is clearer IMO.
The output of getNthFibonacciTerm is actually off by one term (assuming the sequence starts with 0 at index 0). It outputs 1 for input 3. The conditional checks before the loop could also be avoided to simplify the code.
Since the Fibonacci sequence is non-negative, the output could use an unsigned integer to allow slightly larger inputs.

About input range: since the output exceeds the limit of unsigned long long when the input is over 93, I do not see how it can be accurately represented unless you try to implement representations of big integers yourself in C.
About performance: there are several \$\Theta(\log n)\$ algorithms for computing Fibonacci numbers for a given input \$n\$. If you want that, here is a list of all algorithms.


Here is an improved version:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

static const int MinimumTerm = 0;
static const int MaximumTerm = 93;
static const int MaxTries = 3;
static const int InputFailure = -1;

static int getTermCount()
{
    const char *fmtstr = "Please enter an integer value between %d and %d\n";

    for (int count = 0; count &lt; MaxTries; count++)
    {
        int term;

        printf(fmtstr, MinimumTerm, MaximumTerm);    // IMO it is fine to just output to stdout before all attempts fail
        scanf("%d", &amp;term);
        if (term &gt;= MinimumTerm &amp;&amp; term &lt;= MaximumTerm)
        {
            return term;
        }
    }

    return InputFailure;
}

static unsigned long long getNthFibonacciTerm(int term)
{
    unsigned long long nthValue = 0;
    unsigned long long nextValue = 1;  // (n+1)th value, you may find a better name for that

    for (int termCount = 0; termCount &lt; term; termCount++)
    {
        unsigned long long newValue = nthValue + nextValue;
        nthValue = nextValue;
        nextValue = newValue;
    }

    return nthValue;
}

int main()
{
    int term = getTermCount();
    if (term == InputFailure)
    {
        fprintf(stderr, "Invalid Input\n");
        return EXIT_FAILURE;
    }

    printf("The %d term of the Fibonacci Sequence is %llu\n", term, getNthFibonacciTerm(term));
    return EXIT_SUCCESS;
}