Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a value returning function named sumOfEvens that takes two int parameters.

ID: 3843735 • Letter: W

Question


Write a value returning function named sumOfEvens that takes two int parameters. It returns the sum of the squares of all even numbers between the first and second parameter. For example, if the first parameter is 6 and the second is 13, the function returns the value 6 + 8 + 10 + 12. If the parameters are 5 and 8, the function returns the value of 6 + 8. If the parameters are 13 and 2, the first parameter is larger, so the function returns 0. a) Give the function prototype: b) Give an example of the function call: c) Write the function definition (header and body):

Explanation / Answer

I am Implementing application by using java

a)function prototype

public int sumOfEvens(int num1,num2);

b)function call

sumOfEvens(6, 13);

c)function defination

public int sumOfEvens(int num1, int num2) {
       /*
       * if first parameter larger it will return 0
       *
       */
       if (num1 > num2) {
           return 0;
       }
       /*
       * sum stores the total of all even numbers
       */
       int sum = 0;
       for (int i = num1; i <= num2; i++) {
           if (i % 2 == 0) {

               sum = sum + i;
           }
       }
       return sum;
   }

//-------------------------------------------------

Full application with two test cases

public class CaliculatingSumOfEvens {
   public int sumOfEvens(int num1, int num2) {
       /*
       * if first parameter larger it will return 0
       *
       */
       if (num1 > num2) {
           return 0;
       }
       /*
       * sum stores the total of all even numbers
       */
       int sum = 0;
       for (int i = num1; i <= num2; i++) {
           if (i % 2 == 0) {

               sum = sum + i;
           }
       }
       return sum;
   }

   public static void main(String[] args) {
       CaliculatingSumOfEvens caliculatingSumOfEvens = new CaliculatingSumOfEvens();
       /*
       * Test case 1
       */
       System.out.println(caliculatingSumOfEvens.sumOfEvens(6, 13));
       /*
       * Test case 2
       */
       System.out.println(caliculatingSumOfEvens.sumOfEvens(13, 2));
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote