Answer:g
public static int addOddMinusEven(int start, int end){
int odd =0;
int even = 0;
for(int i =start; i<end; i++){
if(i%2==0){
even = even+i;
}
else{
odd = odd+i;
}
}
return odd-even;
}
}
Step-by-step explanation:
Using Java programming language:
- The method addOddMinusEven() is created to accept two parameters of ints start and end
- Using a for loop statement we iterate from start to end but not including end
- Using a modulos operator we check for even and odds
- The method then returns odd-even
- See below a complete method with a call to the method addOddMinusEven()
public class num13 {
public static void main(String[] args) {
int start = 2;
int stop = 10;
System.out.println(addOddMinusEven(start,stop));
}
public static int addOddMinusEven(int start, int end){
int odd =0;
int even = 0;
for(int i =start; i<end; i++){
if(i%2==0){
even = even+i;
}
else{
odd = odd+i;
}
}
return odd-even;
}
}