StefanTobler

Lesson 34 Activity 5

Dec 8th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. /*
  2.  * Lesson 34 Coding Activity 5
  3.  *
  4.  * For the Lesson 34 activities, you will be asked to write one or more methods.
  5.  * Use the template to write a main method that tests each of your methods,
  6.  * then paste everything into the code runner box. Your submission should
  7.  * begin with the first import statement and end with the final }.
  8.  *  
  9.  * Write a method that takes an array of ints
  10.  * and returns a sum of only the even values.
  11.  *
  12.  *     public static int sumEven(int [] a)
  13.  *
  14.  * For example, sumEven(a) would return 6 if a = {1, 2, 3, 4, 5}.
  15.  */
  16.  
  17.  
  18. import java.util.Scanner;
  19.  
  20. class Lesson_34_Activity_Five {
  21.  
  22.    public static int sumEven(int [] a)
  23.     {
  24.      int even = 0;
  25.     for (int i = 0; i < a.length; i++)
  26.      {
  27.        if (a[i]%2 == 0)
  28.          even += a[i];
  29.      }
  30.      return even;
  31.     }
  32.  
  33.     public static void main(String[] args)
  34.      {
  35.      int a[] = {1,2,3,4,5};
  36.      System.out.println(sumEven(a));
  37.     }
  38. }
Add Comment
Please, Sign In to add comment