Thursday, April 19, 2018

Write a program to sum values of an array !!

// Method-I:
  1. public class SumValueOfArrayAlgorithm {
  2. public static void main(String[] args) {
  3. int a[] = { 1, 3, 4, 5, 56, 4 };
  4. int sum = 0;
  5. for (int i : a) {
  6. sum = sum + i;
  7. }
  8. System.out.println(sum);
  9. }
  10. }
Output: 73


// Method-II:
  1. public class SumValueOfArrayAlgorithm {
  2. public static void main(String[] args) {
  3. int a[] = { 1, 3, 4, 5, 56, 4 };
  4. int sum = 0;
  5. for (int i = 0; i < a.length; i++) {
  6. sum = sum + a[i];
  7. }
  8. System.out.println(sum);
  9. }
  10. }
Output: 73

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...