C# Sharp Exercises: Find the sum of all elements of array
C# Sharp Array: Exercise-3 with Solution
Write a program in C# Sharp to find the sum of all elements of array.
Sample Solution:-
C# Sharp Code:
using System;
public class Exercise3
{
public static void Main()
{
int[] a= new int[100];
int i, n, sum=0;
Console.Write("\n\nFind sum of all elements of array:\n");
Console.Write("--------------------------------------\n");
Console.Write("Input the number of elements to be stored in the array :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input {0} elements in the array :\n",n);
for(i=0;i<n;i++)
{
Console.Write("element - {0} : ",i);
a[i] = Convert.ToInt32(Console.ReadLine());
}
for(i=0; i<n; i++)
{
sum += a[i];
}
Console.Write("Sum of all elements stored in the array is : {0}\n\n", sum);
}
}
Sample Output:
Find sum of all elements of array: -------------------------------------- Input the number of elements to be stored in the array :4 Input 4 elements in the array : element - 0 : 2 element - 1 : 4 element - 2 : 6 element - 3 : 4 Sum of all elements stored in the array is : 16
Flowchart:
C# Sharp Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a program in C# Sharp to read n number of values in an array and display it in reverse order.
Next: Write a program in C# Sharp to copy the elements one array into another array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework