Write a program in C# Sharp to calculate the sum of elements in an array

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to calculate the sum of elements in an array.

Test Data :
Input 5 elements in the array :
element - 0 : 5
element - 1 : 7
element - 2 : 3
element - 3 : 2
element - 4 : 9
Expected Output :
The sum of the elements of the array is 26

الأجوبة

using System;
public class funcexer5
  {
    public static int Sum(int[] arr1)
    {
        int tot=0;
        for (int i = 0;i < arr1.Length; i++)
        tot += arr1[i];
        return tot;
    }
    public static void Main()
    {
       int[] arr1 = new int[5];
       Console.Write("\n\nFunction : Calculate the sum of the elements in an array :\n");  
       Console.Write("--------------------------------------------------------------\n");         
       Console.Write("Input 5 elements in the array :\n");    
       for(int j=0; j<5; j++)    
       {    
        Console.Write("element - {0} : ",j);  
        arr1[j] = Convert.ToInt32(Console.ReadLine());         
       }    
       Console.WriteLine("The sum of the elements of the array is {0}", Sum(arr1));
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...