Sum of Array Elements

import "dart:io";//1
main()
{
  print("Enter the Size of Array");//2
  var size = int.parse((stdin.readLineSync()));//3
  var numbers=List(size);//4
  for(int i=0;i<size;i++)//5
    {
       numbers[i]=int.parse(stdin.readLineSync());//6
    }
  var sumofArrayElements = 0;//7
  for(int i=0;i<numbers.length;i++)//8
    {
             sumofArrayElements+= numbers[i];//9
    }
  print("Sum  of Given Array is=$sumofArrayElements");//10
}
  1. Import statement for input and output library
  2. Instruct the user to enter the size of the array
  3. After converting user input into an int and then assigned to size variable
  4. Create a new array(List ) of user-entered size with a variable name numbers
  5. Execute for loop from 0 to less than numbers length
  6. After converting user input into int and then assigned to the element of array (initialization of Array Elements based on user input)
  7. Create and initialized and sumofArrayElement variable with 0
  8. Execute for loop from 0 to less than numbers length to calculate the sum of array elements
  9. Add elements value in sumofArrayElements
  10. Print the sum of Array on Console

Output

Enter the Size of Array
5
10
45
78
90
34
Sum of Given Array is=257

Leave a Reply

Your email address will not be published. Required fields are marked *