Lab No3: Basic Programming
with Arrays and Functions in C#
Introduction
Lab Objectives
This lab aims to help students strengthen their fundamental programming skills in C# through
hands-on exercises focused on arrays and functions. By completing these exercises, students
will:
Practice using arrays to store and manipulate data.
Implement functions to organize and modularize code.
Apply fundamental programming concepts such as loops, conditionals, and input/output
operations.
List of Exercises
1. Ex 1: Sum of the First and Last Elements in an Array
2. Ex 2: Print All Even Numbers in an Array
3. Ex 3: Declare and Call Functions (Sum of Even Numbers)
4. Ex 4: Simple To-Do List Using an Array
pg. 1
Ex 1: Sum of the First and Last Elements in
an Array
Objective
Write a program that accepts an array of integers from the user and prints the sum of the
first and last elements.
Description
In this exercise, students will create a program that prompts the user for the number of
elements and each element in the array. The program then calculates and displays the sum of
the first and last elements in the array.
Example Input-Output
Input: Array of integers, e.g., [10, 2, 3, 4, 5]
Output: The sum of the first and last elements is: 15
Pseudocode
pg. 2
pg. 3
Ex 2: Print All Even Numbers in an Array
Objective
Write a program that accepts an array of integers and prints all the even numbers in the
array.
Description
This exercise instructs students to prompt the user to enter an array of integers. The program
then checks each element to determine if it is even and, if so, prints it to the screen.
Example Input-Output
Input: Array of integers, e.g., [10, 15, 22, 33, 40]
Output: 10 22 40
Pseudocode
pg. 4
Ex 3: Declare and Call Functions
(Sum of Even Numbers)
Objective
Write a program that demonstrates the use of functions by defining and calling a function to
calculate the sum of even numbers in an array.
Description
Students will learn to modularize code by creating a function named SumOfEvenNumbers.
This function will calculate the sum of all even numbers in an array and return the result. The
function will be called from the Main method.
Example Input-Output
Input: Array of integers, e.g., [10, 15, 22, 33, 40]
Output: The sum of even numbers is: 72
Pseudocode
pg. 5
Ex 4: Simple To-Do List Using an Array
Objective
Build a simple To-Do List application that allows users to add and list tasks using an array.
Description
In this exercise, students will create a To-Do List where they can add tasks (up to a set limit)
and view all tasks added so far. This lab demonstrates how to use arrays and functions to
manage user inputs.
Example Input-Output
Input: 1 (Add Task), Buy groceries
Output: Your tasks: 1. Buy groceries
Pseudocode
pg. 6