Write a value returning function named sumOfEvens that takes two int parameters.
ID: 3843735 • Letter: W
Question
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));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.