C# Sharp Exercises: Find the cumulative sum of an array of number
C# Sharp Basic: Exercise-85 with Solution
Write a C# Sharp program to find the cumulative sum of an array of number.
A cumulative sum is a sequence of partial sums of a given sequence. For example, the cumulative sums of the sequence {x, y, z,...}, are x , x+y , x+y+z
Sample Solution:
C# Sharp Code:
using System;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
double[] nums = {1, 3, 4, 5, 6, 7};
Console.WriteLine("\nOrginal Array elements: ");
foreach (var item in nums)
{
Console.Write(item.ToString()+" ");
}
Console.WriteLine("\nCumulative sum of the said array elements:");
double[] result = test(nums);
foreach (var item in result)
{
Console.Write(item.ToString()+" ");
}
double[] nums1 = { 1.2, -3, 4.1, 6, -5.47 };
Console.WriteLine("\nOrginal Array elements: ");
foreach (var item in nums1)
{
Console.Write(item.ToString() + " ");
}
Console.WriteLine("\nCumulative sum of the said array elements:");
double[] result1 = test(nums1);
foreach (var item in result1)
{
Console.Write(item.ToString() + " ");
}
}
public static double[] test(double[] nums)
{
for (int i = 1; i < nums.Length; i++)
{
nums[i] = nums[i] + nums[i - 1];
}
return nums;
}
}
}
Sample Output:
Orginal Array elements: 1 3 4 5 6 7 Cumulative sum of the said array elements: 1 4 8 13 19 26 Orginal Array elements: 1.2 -3 4.1 6 -5.47 Cumulative sum of the said array elements: 1.2 -1.8 2.3 8.3 2.83
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous:Write a C# Sharp program to get the index number of all lower case letters in a given string.
Next: Write a C# Sharp program to get the number of letters and digits in a given string.
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