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

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...