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

Introduction

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

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

Leave a Comment

x