Basic Programming C# Examples

C# Program to Compute Sum of all Elements of an array of integers

The Program in C# Program to Compute Sum of all Elements of an array of integers is given below:

using System;

namespace ArraySum
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, Enter the integers below to find their sum: ");
            Console.WriteLine("Well, enter the number of integers in the array here: ");
            int n = Convert.ToInt32(Console.ReadLine());

            int[] arr = new int[n];
            int sum = 0;

            Console.WriteLine("Well,enter the elements of the array here: ");
            for (int i = 0; i < n; i++)
            {
                arr[i] = Convert.ToInt32(Console.ReadLine());
                sum += arr[i];
            }

            Console.WriteLine("Okay, the sum of the elements of the array is: " + sum);
            Console.ReadLine();
        }
    }
}

Output:

Hello Codeauri Family, Enter the integers below to find their sum: Well, enter the number of integers in the array here: 3
Well,enter the elements of the array here:
123
908
17
Okay, the sum of the elements of the array is: 1048

Pro-Tips💡

Here are the step by step execution of above program:

  1. Imports the System namespace to use classes like Console and Convert.
  2. Defines a Program class within a ArraySum namespace.
  3. Within the Main method of the Program class, the program outputs a message asking the user to enter the number of integers in the array. The user input is read using Console.ReadLine and converted to an integer using Convert.ToInt32. This integer value n represents the number of elements in the array.
  4. The program declares an array arr with n elements and a variable sum initialized to 0.
  5. The program outputs a message asking the user to enter the elements of the array and uses a for loop to read n elements of the array. Each element is read using Console.ReadLine and converted to an integer using Convert.ToInt32. The sum of each element is computed by adding the current element to the running sum stored in the sum variable.
  6. After the for loop, the program outputs the sum of all elements in the array using Console.WriteLine.
  7. Finally, the program waits for user input using Console.ReadLine.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X