Our Even Numbers code in C#.
Even numbers Class File:
using System.Collections.Generic;
namespace Arithmetic_CS
{
class EvenNumbers
{
private int start; // Our starting point
private int stop; // where we will stop
private List<int> list_of_primes; // We will house our gathered prime numbers here.
// Our constructor
public EvenNumbers(int first, int last)
{
start = first;
stop = last;
list_of_primes = new List<int>();
}
public List<int> prepResult()
{
/*
* Loop from start to stop and rip out even numbers;
*/
while (start <= stop)
{
if (start % 2 == 0)
{ // modulo(%) is explained later
list_of_primes.Add(start);
}
start = start + 1; // increase 'start' by 1
}
return list_of_primes;
}
}
}
Main File:
using System;
using System.Collections.Generic;
namespace Arithmetic_CS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to our demonstration sequels");
Console.WriteLine("Hope you enjoy (and follow) the lessons.");
Console.WriteLine("");
/* Use the Even Number class. */
int first = 1;
int last = 100;
List<int> answer;
EvenNumbers even_data = new EvenNumbers(first, last); // even numbers between 1 and 100
answer = even_data.prepResult();
Console.WriteLine("Even numbers between " + first + " and " + last + " are:");
Console.WriteLine(String.Join(", ", answer));
}
}
}
Try it out!
Elegance (0.0)