Answers for "how to sum array in flutter"

0

how to sum array in flutter

main(List<String> args) {
  var sum = 0;
  var given_list = [1, 2, 3, 4, 5];
  for (var i = 0; i < given_list.length; i++) {
    sum += given_list[i];
  }

  print("Sum (for loop): ${sum}");

  sum = 0;
  given_list.forEach((e) => sum += e);
  print("Sum (forEach): ${sum}");

  sum = given_list.reduce((value, element) => value + element);
  print("Sum (reduce): ${sum}");

  sum = given_list.fold(0, (previous, current) => previous + current);
  print("Sum (fold): ${sum}");
}
Posted by: Guest on September-15-2021
0

how to sum array in flutter

main(List<String> args) {
  var given_list = [1, 2, 3, 4, 5];

  var sum = given_list.reduce((value, element) => value + element);

  print("Sum : ${sum}");
}
Posted by: Guest on September-15-2021

Code answers related to "how to sum array in flutter"

Browse Popular Code Answers by Language