Click here to Skip to main content
14,644,624 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello can you please help me with the following array in java
A FOR loop that will cycle through each of these elements and add them together ..displaying a sentence that will notify the user of the total value of the elements in the array with the end of the algorithm...

sequence: 2,8,18,32,50,72,98,128,168,200
Posted
Updated 7-May-14 21:35pm
v3
Comments
DamithSL 8-May-14 3:14am
   
in which programming language?
   
No language, not platform, nothing. What's the point asking such "question", and what's the use?
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try:
int[] data = new int[] {2,8,18,32,50,72,98,128,168,200};
int total = 0;
for (int index = 0; index < data.Length; index++)
   {
   total += data[index];
   }
Console.WriteLine(total);

But a better way:
int[] data = new int[] {2,8,18,32,50,72,98,128,168,200};
Console.WriteLine(data.Sum(x => x));
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

You need to check out the documentation of your favourite language, in order to actually coding it.
A simple approach would be:
  • Initialize the sum to 0.
  • Iterate over the 10 items, that is make a FOR loop with an index, say i, ranging from 0 to 9 (or 1 to 10, depending on the programming language).
  • At every iteration update the sum, someting like sum = sum + a[i].
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100