sum = number[0] + number[1] + number[2] + number[3] + number[4];
sum = 0; // Starting value for the sum
for (i=0; i<=4; i++) // And we calculate it in an iterative way
sum += number[i];
/*---------------------------*/
/* C# Example #35: */
/* example35.cs */
/* */
/* Arrays example #3 */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example35
{
public static void Main()
{
int[] number = // An array for 5 integers
{200, 150, 100, -50, 300};
int sum; // An integer for the sum
int i; // To iterate through the elements
sum = 0; // Starting value for the sum
for (i=0; i<=4; i++) // And we calculate it in an iterative way
sum += number[i];
Console.WriteLine("Their sum is {0}", sum);
}
}
No comments:
Post a Comment