Click here to Skip to main content
15,875,568 members
Please Sign up or sign in to vote.
1.00/5 (1 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?
Sergey Alexandrovich Kryukov 8-May-14 3:15am    
No language, not platform, nothing. What's the point asking such "question", and what's the use?
—SA

Try:
C#
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:
C#
int[] data = new int[] {2,8,18,32,50,72,98,128,168,200};
Console.WriteLine(data.Sum(x => x));
 
Share this answer
 
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].
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900