Display the n terms of even natural numbers(between 1 and 100) and their sum This is a C# console application that displays the n terms of even natural numbers between (1 and 100) and their sum

Is this good? Or is it bug riddled ?

int numberOfEvenNumbers;
int currentNumber;
currentNumber = 0;
int counter;
counter = 1;
int sum;
sum = 0;

Console.Write("How many even numbers :");

numberOfEvenNumbers = Convert.ToInt32(Console.ReadLine());

while (counter &lt;= 100)
{


    if (counter % 2 == 0)
    {
        Console.WriteLine(counter);
        sum += counter;
        currentNumber++;
    }

    if (currentNumber == numberOfEvenNumbers)
    {
        break;
    }

    counter++;
}


Console.Write("Sum : ");
Console.WriteLine(sum);

Console.ReadLine();
It is neither good nor bad. It does the job: it shows a list of even numbers up to a limit lesser than or equal to 100. But the design or architecture of the program is ugly, even for a small program like this.



Previous it was common to declare all variables at the top of the scope (function or other bracketed section) (and some languages require that), but now in C# it is considered standard to declare the variable when they are used the first time.




  numberOfEvenNumbers = Convert.ToInt32(Console.ReadLine());


This is quick and dirty and will crash the program if the user enters a non numeric string.

So you'll have to check the input and react to an invalid such:

  int numberCount = 0;
  string prompt = "How many even numbers [Enter a number lesser than or equal to 50]: ";
  string errorMessage = "";
  do
  {
    Console.Write($"{errorMessage}{prompt}");
    errorMessage = "Invalid input. ";
  }
  while (!int.TryParse(Console.ReadLine(), out numberCount) || numberCount &lt;= 1 || numberCount &gt; 50);


Here I validate (&gt; 1 and &lt; 100) the input in the UI which is preferable to do compared to validating while running the algorithm.



The header of the question says


  numbers between 1 and 100


(excluding 100) but you iterate potentially up to and including 100:


  while (counter &lt;= 100) { ... }


so what do you really mean?




if (counter % 2 == 0)
{



Even numbers can be found by checking if a number is divisible by 2 as you do, but the sequence of even numbers from 2 and upwards can be found like this:

  for (int i = 2; i &lt;= limit; i += 2)
  {
    Console.WriteLine(i);
  }


in this way you are almost done with one variable (i) and a limit, you just need a sum to sum up with:

// The 6 first even numbers:
int numberCount = 6 * 2;
int sum = 0;    

for (int i = 2; i &lt;= numberCount; i += 2)
{
  Console.WriteLine(i);
  sum += i;
}
Console.WriteLine(sum);




So all in all it ends up like this:

static void EvenNumberCount()
{
  int numberCount = 0;
  string prompt = "How many even numbers [Enter a number lesser than 50]: ";
  string errorMessage = "";
  do
  {
    Console.Write($"{errorMessage}{prompt}");
    errorMessage = "Invalid input. ";
  }
  while (!int.TryParse(Console.ReadLine(), out numberCount) || numberCount &lt;= 1 || numberCount &gt; 50);

  numberCount *= 2;
  int sum = 0;

  for (int i = 2; i &lt;= numberCount; i += 2)
  {
    Console.WriteLine(i);
    sum += i;
  }

  Console.WriteLine();
  Console.WriteLine($"Sum: {sum}");
}


But here I do too much in the same method:

The program has 3 steps:


User input
The algorithm
Display of result


This calls for 3 separate methods or functions:


limit = GetCountOfNumbers();
result = EnumerateAndEvenNumbers(limit);
ShowResult(result)


Which could result in something like:

static int GetCountOfNumbers()
{
  int numberCount = 0;
  string prompt = "How many even numbers [Enter a number lesser than or equal to 50]: ";
  string errorMessage = "";
  do
  {
    Console.Write($"{errorMessage}{prompt}");
    errorMessage = "Invalid input. ";
  }
  while (!int.TryParse(Console.ReadLine(), out numberCount) || numberCount &lt;= 1 || numberCount &gt; 50);

  return numberCount;
}

static (int Sum, IEnumerable&lt;int&gt; EvenNumbers) EnumerateAndSumEvenNumbers(int numberCount)
{
  int sum = 0;
  List&lt;int&gt; evenNumbers = new List&lt;int&gt;();
  numberCount *= 2;

  for (int i = 2; i &lt;= numberCount; i += 2)
  {
    evenNumbers.Add(i);
    sum += i;
  }

  return (sum, evenNumbers);
}

static void ShowResult(int limit, int sum, IEnumerable&lt;int&gt; evenNumbers)
{
  Console.WriteLine();
  Console.WriteLine($"The first {limit} even numbers are: ");
  foreach (int number in evenNumbers)
  {
    Console.WriteLine(number);
  }

  Console.WriteLine();
  Console.WriteLine($"Sum: {sum}");
}

static void TestNumberCount()
{
  int numberCount = GetCountOfNumbers();
  (int Sum, IEnumerable&lt;int&gt; EvenNumbers) = EnumerateAndSumEvenNumbers(numberCount);
  ShowResult(numberCount, Sum, EvenNumbers);
}




By the way: The sum of any number n of even numbers from 2 and upwards can be found as: sum = n * (n + 1). This could simplify the above "algorithm" a bit.