Q:

Write C# program to find sum of all elements of an array

0

Write C# program to find sum of all elements of an array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
class Program
{
    static void Main()
    {       
        int[] arr = new int[100];
        int i, num, sum = 0;
 
        ////Reads size and elements in array
        Console.WriteLine("Enter size of the array: ");
        num = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter "+num+" elements in the array: ");  
 
        for(i=0; i<num; i++)
        {
            arr[i] = Convert.ToInt32(Console.ReadLine());            
        }
 
        //Adding all elements        
        for(i=0; i<num; i++)
        {
            sum = sum + arr[i]; // Calculating sum
        }
 
        Console.WriteLine("Sum of all elements of array: "+sum);
 
        Console.ReadLine();
    }
 
}

Result:

Enter size of the array: 

5

Enter 5 elements in the array: 

1

2

3

4

5

Sum of all elements of array: 15

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C# program to count even and odd elements in... >>
<< Write C# program to count total number of negative...