title: Identify Sequence and Provide 4th Integer
body: <p>I was tasked to solve this challenge on a programming exam, but I'm not sure that I have properly solved it. I am inquisitive about what really is the solution to this task:</p>
  <p><strong>Make an application that will tell the user to input 3 numbers, identify the sequence, and provide the 4th integer. It should prompt if no relations is found</strong></p>
  <p><em>Note: Possible operators may include addition, subtraction, multiplication, division, exponent, and modulo division.</em></p>
  <p>Sample Output:</p>
  <p>2, 4, 6, <strong>8</strong></p>
  <p>10, 9, 7, <strong>4</strong></p>
  <p>1, 4, 9, <strong>16</strong></p>
  <p>1, 1, 2, <strong>6</strong></p>
  <p>120, 120, 60, <strong>20</strong></p>
  <p><strong>Here is my solution so far:</strong></p>
  <pre><code>using System;
  using System.IO;
  
  namespace TestQuestionnaire
  {
      class Program
      {
          static void Main(string[] args)
          {
              var numbers = new int[3];
              string answer;
              bool exit = false;
  
              
              while (!exit)
              {
                  // Loop for 5 times
                  for (int n = 0; n &lt;= 5; n++)
                  {
                      // User Input
                      Console.WriteLine(&quot;Please Enter 3 Numbers.&quot;);
                      for (var i = 0; i &lt; numbers.Length; i++)
                      {
                          var error = false;
  
                          while (!error)
                          {
                              Console.Write(&quot;Enter Number: &quot;);
                              error = int.TryParse(Console.ReadLine(), out numbers[i]);
                              if (!error) Console.WriteLine(&quot;Invalid Number! Enter a Valid Number and Try Again. &quot;);
                          }
                      }
                      NextNumber(numbers);
                  }
  
                  // Prompt for exit
                  Console.WriteLine(&quot;Finished! Try Again? Y/N&quot;);
                  answer = Console.ReadLine()?.ToUpper();
                  if (answer == &quot;N&quot;)
                  {
                      exit = true;
                  }
  
              }
  
             
          }
  
          private static void NextNumber(int[] numbers)
          {
              int next = 0;
              int interval_one = 0;
              int interval_two = 0;
  
              // determine if first two number is descending, ascending or equal (addition and subtraction)
              if (numbers[0] &lt; numbers[1])
              {
                  interval_one = (numbers[1] - numbers[0]);
  
                  if (numbers[1] &gt; numbers[2])
                  {
                      interval_two = (numbers[1] - numbers[2]);
                  }
                  else
                  {
                      interval_two = (numbers[2] - numbers[1]);
                  }
  
                  //determine if constant interval (eg, 2,4,6,8 or 1,2,3,4) otherwise evaluate more
                  if (interval_one == interval_two)
                  {
                      next = numbers[2] + interval_two;
                  }
                  else
                  {
                      next = numbers[2] + interval_two + (interval_two - interval_one);
                  }
              }
              else if (numbers[0] &gt; numbers[1])
              {
                  interval_one = numbers[0] - numbers[1];
  
                  if (numbers[1] &gt; numbers[2])
                  {
                      interval_two = (numbers[1] - numbers[2]);
                  }
                  else
                  {
                      interval_two = (numbers[2] - numbers[1]);
                  }
  
                  if (interval_one == interval_two)
                  {
                      next = numbers[2] - interval_two;
                  }
                  else
                  {
                      next = numbers[2] - interval_two + (interval_one - interval_two);
                  }
              }
              else
              {
                  if (numbers[0]/1 == numbers[1] &amp;&amp; numbers[1]/2 == numbers [2])
                  {
                      next = numbers[2] / 3;
                  }
                  else if (numbers[0] * 1 == numbers[1] &amp;&amp; numbers[1]*2 == numbers[2])
                  {
                      next = numbers[2] * 3;
                  }
              }
  
              Console.WriteLine(&quot;Predicted 4th Number: {0}&quot;, next);
  
          }
  
      }
  }
  </code></pre>
  <p>Thanks!</p>