C# Arrays: Input Numbers and Find the Sum (Beginner Guide with User Input)
Table of Contents
Introduction of the Lesson
In C# programming, arrays are used to store multiple values in a single variable. Instead of creating separate variables for each value, arrays allow you to group related data together, making your program more organized and efficient. Arrays are especially useful when working with lists of numbers, such as student scores, product prices, or survey results.
This lesson focuses on a fundamental yet important task: accepting multiple numbers from the user and computing their total sum using an array. This activity introduces key programming concepts such as user input, loops, indexing, and accumulation. By mastering this, you establish a strong foundation for more advanced topics like data processing and algorithm design.
Learning Objectives
To guide your learning effectively, this lesson follows Outcome-Based Education (OBE) principles. These objectives ensure that you gain both theoretical understanding and practical skills.
By the end of this lesson, you should be able to:
- Understand how arrays store multiple values in C#
- Learn how to accept user input and store it in an array
- Practice using loops to process array elements
- Apply array concepts to solve real-world problems like summation
These objectives are designed to move you progressively from knowledge acquisition to practical implementation. You will not only understand how arrays work but also develop the ability to apply them in real programming scenarios.
Beginner-Friendly Source Code (with User Input)
Instructions:
- Open Visual Studio or any C# IDE
- Create a new Console Application
- Copy and paste the code below
- Run the program and follow the prompts
using System;
namespace ArraySumApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Array Sum Calculator ===");
Console.Write("How many numbers do you want to input? ");
int size = Convert.ToInt32(Console.ReadLine());
int[] numbers = new int[size];
int sum = 0;
// Input values
for (int i = 0; i < size; i++)
{
Console.Write($"Enter number {i + 1}: ");
numbers[i] = Convert.ToInt32(Console.ReadLine());
}
// Calculate sum
for (int i = 0; i < size; i++)
{
sum += numbers[i];
}
Console.WriteLine("The total sum is: " + sum);
Console.ReadLine();
}
}
}
Example Output
=== Array Sum Calculator ===
How many numbers do you want to input? 4
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
The total sum is: 100
Lesson Summary
This lesson introduced the concept of arrays in C# and demonstrated how to use them to store multiple user inputs efficiently. By iterating through the array using loops, we were able to calculate the total sum of all elements. This exercise reinforced essential programming concepts such as indexing, iteration, and accumulation. Mastering these fundamentals prepares you for more advanced topics like sorting, searching, and data analysis using arrays.
Multiple Choice Questions
- What is an array in C#?
a. A single variable storing one value
b. A collection of multiple values stored in one variable
c. A loop structure
d. A method - Why are arrays useful?
a. They reduce the need for loops
b. They allow storing multiple related values efficiently
c. They eliminate variables
d. They only work with strings - Which loop is commonly used to access array elements?
a. if statement
b. switch statement
c. for loop
d. try-catch - What does the variable
sumrepresent in the program?
a. The number of elements
b. The total of all array values
c. The index value
d. The last input - Which improvement can make the program more robust?
a. Removing loops
b. Adding input validation
c. Avoiding arrays
d. Using only one variable
Exercises, Assessment, and Lab Exam
To fully understand arrays, it is important to go beyond simple examples and explore different variations. The following activities are designed to strengthen your ability to manipulate arrays and handle user input effectively.
Exercises
- Modify the program to calculate the average of the numbers
- Display all numbers entered by the user
- Identify and display the largest and smallest numbers
Assessment
- Create a program that:
- Counts how many numbers are even and odd
- Calculates the product of all elements
- Explain the difference between array and list in C#
Lab Exam
- Develop an Array Processing System:
- Accept at least 10 numbers
- Display sum, average, max, and min
- Sort numbers (ascending/descending)
- Include input validation and error handling
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.