usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Simple Code for Listing Even Numbers in C# - Fun Exercise for Young Students



What are Even Numbers? | Maths Explanation for C# Kids

Hello pupils, what are even numbers?
Answer:
Even numbers are numbers that are divisible by 2.

They include:

2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, ...

Considering how simple and straight-forward even numbers are, writing a C# code for even numbers serves as a great way to introduce our Mathematics educational activities for young learners.
Well then, let's see how we can write a programming code to make our computer list a set of even numbers in the C# language, between a given range of values. Bear in mind, we use only a loop and a simple conditional statement for the C# code.

Create a new c# project; call it Arithmetic.
Create a new c# class file; call it EvenNumbers.

Type out the adjoining C# code for listing even numbers.


How to run C# Codes

Run the C# code from the main function class by pressing key f5 or the Start symbol.



So! C# Fun Practice Exercise - List Even Numbers

As a fun practice exercise, feel free to try out your own boundary values, and see how the C# code lists the even numbers between those boundary values.







C# Code for Even Numbers class.

using System.Collections.Generic;

namespace Arithmetic
{
    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;
        }
    }
}

C# Code for Even Numbers - Main Class.

using System;
using System.Collections.Generic;

namespace Arithmetic
{
    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));

        }
    }
}



<< PreviousNext >>