Sunday, 20 November 2022

Array - Exercise 1 , Calculate and return the sum of all the even numbers present in the numbers array passed to the method calculateSumOfEvenNumbers. Test the functionalities using the main() method of the Tester class.

 Array - Exercise 1


Calculate and return the sum of all the even numbers present in the numbers array passed to the method calculateSumOfEvenNumbers. Implement the logic inside calculateSumOfEvenNumbers() method.  Test the functionalities using the main() method of the Tester class.     Sample Input and Output








class Tester{
public static int calculateSumOfEvenNumbers(int[]numbers){
int sum=0;
for(int i=0;i<numbers.length;i++)
{
int a=numbers[i]%2;
if(a==0)
{
sum=sum+numbers[i];
}
}
returnsum;
}
public static void main(String[]args){
int[]numbers={68,79,86,99,23,2,41,100};
System.out.println("Sum of even numbers: "+calculateSumOfEvenNumbers(numbers));
}
}



No comments:

Post a Comment

Free Resume Template

Creating a blog post that discusses the pros and cons of using a resume rule is a great way to provide valuable information to job seekers ...