Q:

Unknown Error in Number Prediction Algorithm

So I'm working on this simple number prediction algorithm as an entry into algorithms and pattern recognition. It takes a linear string of numbers (14 in this case) starting at 2 and going up by 2 until 28.
The program works out the difference between each number by subtracting the number before from it. It then checks all the differences are the same, then adds the difference to the last number and prints it to the screen.
It works fine except that it thinks the difference is 0 every time and so prints the last number, 28, and the next number. There seem to be other questions like this, except they are asking how to do it with non-linear sequence and no one has the problem I do.
I've tried everything I can think of, but it still can't determine the difference. It's probably something really obvious that I'm missing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace number_predition_with_constant
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] sequence = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28 }; //all differ by 2. Diff = 2.
            Console.WriteLine(sequence);
            Console.WriteLine("");
            int[] differences = {};
            int legnth = sequence.Length;
            int diff = 0;  //when not given value,  some other instances not recognised 
            int j = 0;
            //find difference between each number.
            for (int i = 0; i == legnth-1; i++)
            {
                j = i + 1;
                diff = sequence[j] - sequence[i];
                differences[i] = diff;
            }
            //Print the difference between each number.
            Console.Write("Difference: ");
            Console.WriteLine(diff);
            //Check all diffs are same. If not the same, print "Error"
            for (int i = 0; i == legnth-1; i++)
            {
                if (differences[i] != differences[i+1])
                {
                    Console.WriteLine("Error");
                }
            }
            //calculate next number and print.
            Console.Write("There are: ");
            Console.Write(legnth);
            Console.WriteLine(" Numbers in the sequence");
            legnth = legnth - 1;
            int next = sequence[legnth] + diff;
            Console.Write("The next Number in the sequence is: ");
            Console.WriteLine(next);
            Console.ReadKey(); //Stop Console from closing till key pressed
            }
    }
}

A:

I think that this line (which occurs in two places)
for (int i = 0; i == legnth-1; i++)

Should be:
for (int i = 0; i <= legnth-1; i++)

Also these lines:
int[] differences = {};
int legnth = sequence.Length;

Should be:
int legnth = sequence.Length;
int[] differences = new int[legnth];

There are probably other problems too, but fix those first. You might also want to globally change legnth to length... ;)
It looks like you might also have some out-by-one errors.
Run the program under the debugger and single step through the entire thing. That should tell you what's wrong and give you a much better understanding of how to fix it.
That will be a useful exercise, and much more beneficial to you than if we just corrected all your code (even if anyone did have the time to do so). 

