A bug in a program I'm writing as an assignment for my java. Objective: program should take two parameters and find all the odd numbers between those parameters. Then it should return the sum of all those numbers.
Bug: When the parameters are negative or starting number is greater than the ending number, it should return -1. But my program is returning 0.
Maybe my program isn't updating the sum value inside the loop. But even so, as per return condition, my function should return -1, not the sum.
public class SumOddRange{
public static void main(String[] args){
System.out.println(sumOdd(10,5));
}
public static boolean isOdd(int number){
return (number<0)||(number%2==0)?false:true;
}
public static int sumOdd(int start, int end){
int sum = 0;
for(int i=start; i<=end; i++){
if(isOdd(i)){
sum+=i;
}
}
return (start<=0)&&(start>end)? -1: sum;
}
}
-1before even getting to the loopif(start < 0 || end < 0 || start > end) return -1;