Advertisement
syad28

WIX1002 15/16 [Q1]

Jan 27th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. /*  University of Malaya
  2.     WIX 1002 - Fundamental of Programming
  3.     Final Exam Sem 1 Year 2015/16
  4.     Question 1 of 5
  5.     Project name : "1516Q1.java"
  6. */
  7.  
  8. package pkg1516q1;
  9. import java.util.*;
  10. public class Main {
  11.     public static void main(String[] args) {
  12.         int[] num = {34,15,12,27,74,23};
  13.         int cnt = 0;
  14.         for(int i = 0;i<num.length;i++)
  15.             if(isEven(num[i]))
  16.                 cnt++;
  17.         System.out.println("The number of even number is " + cnt);
  18.         System.out.println("The sum of the array is "+getTotal(num));
  19.     }
  20.    
  21.     public static boolean isEven(int a){
  22.         if(a%2==0)
  23.             return true;
  24.         else
  25.             return false;
  26.     }
  27.    
  28.     public static int getTotal(int[] a){
  29.         int total=0;
  30.         for(int i=0;i<a.length;i++)
  31.             total+=a[i];
  32.         return total;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement