Function Sum Of Array - C# Programming Exercise

In this C# exercise, you are asked to write a program to calculate the sum of the elements in an array. The Main function should look like the following:


public static void Main()
{
int[] example = {20, 10, 5, 2 };
Console.WriteLine("The sum of the example array is {0}", Sum(example));
}

In this case, the Sum function should accept an array of integers and return the sum of all its elements. This exercise is useful for practicing array manipulation in C#, as well as learning how to perform simple mathematical operations within a program. By working with arrays and functions, you will improve your understanding of how to handle collections of data in C# and how to structure your code to work with them efficiently.

 Category

Functions

 Exercise

Function Sum Of Array

 Objective

Write a C# program to calculate the sum of the elements in an array. "Main" should be like this:

public static void Main()
{ int[] example = {20, 10, 5, 2 };
Console.WriteLine(
__"The sum of the example array is {0}", __Sum(example));
}
}

 Write Your C# Exercise

// Importing the System namespace to access basic system functions
using System;

class Program
{
    // Function to calculate the sum of all elements in the array
    public static int Sum(int[] array)
    {
        // Initialize a variable to store the sum of array elements
        int total = 0;

        // Loop through each element of the array and add it to the total sum
        foreach (int number in array)
        {
            total += number;
        }

        // Return the calculated sum
        return total;
    }

    // Main method where the Sum function is called
    public static void Main()
    {
        // Define an example array with integer values
        int[] example = { 20, 10, 5, 2 };

        // Call the Sum function to calculate the sum of the array elements
        // Then display the result using Console.WriteLine
        Console.WriteLine("The sum of the example array is {0}", Sum(example));
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function double

    In this C# exercise, you are asked to write a function named "Double" to calculate and return an integer number doubled. For example, when calling the Double(7...

  •  Function Double reference parameter

    In this C# exercise, you are asked to write a function named "Double" that calculates the double of an integer number and modifies the data passed as an argument. Thi...

  •  Function swap reference parameters

    In this C# exercise, you are asked to write a function named "Swap" that swaps the values of two integer numbers, which are passed by reference. This is done b...

  •  Function power local variables

    In this C# exercise, you are asked to write a function named "Power" that calculates the result of raising an integer number to another positive integer number. The ...

  •  Function recursive power

    In this C# exercise, you are asked to write a function that calculates the result of raising one integer to another integer using recursion. For example, raising 5 to...

  •  Function Fibonacci

    In this C# exercise, you are asked to write a program that uses recursion to calculate a number in the Fibonacci series. The Fibonacci series is a seque...