.NET Practical Slip 14 Q A

2. Slip-1 Problem Statement

Q. Write a program in C#.Net to find the sum of all elements in the array. 15 Marks

Answer:

using System;

namespace SumOfArrayElements
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define an array with some elements
            int[] numbers = { 10, 20, 30, 40, 50 };

            // Variable to hold the sum of elements
            int sum = 0;

            // Loop through the array and add each element to the sum
            for (int i = 0; i < numbers.Length; i++)
            {
                sum += numbers[i];
            }

            // Display the sum of all elements in the array
            Console.WriteLine("The sum of all elements in the array is: " + sum);
            Console.ReadLine();
        }
    }
}
Spread the love

Leave a Comment

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

Scroll to Top