Sum odd numbers program

Asked by At

I'm writing a program where I'm supposed to make a method that calculates if the passed number is odd. For this method, I also need to check that passed number is > 0, and if not, return false. I am also supposed to make a second method with two parameters (start and end, which represents a range of numbers) and the method is supposed to use a for loop to sum all odd numbers within that range. The parameter end needs to be greater than or equal to start and both start and end parameters have to be greater than 0. If those conditions are not satisfied return -1 from the method to indicate invalid input.

This is the assignment:

Write a method called isOdd with an int parameter and call it number. The method needs to return a boolean. Check that number is > 0, if it is not return false. If number is odd return true, otherwise return false.

Write a second method called sumOdd that has 2 int parameters start and end, which represent a range of numbers. The method should use a for loop to sum all odd numbers in that range including the end and return the sum. It should call the method isOdd to check if each number is odd. The parameter end needs to be greater than or equal to start and both start and end parameters have to be greater than 0. If those conditions are not satisfied return -1 from the method to indicate invalid input.

I've tried for over an hour now but I'm not doing it correctly because my output is wrong. I've written down the expected results and my results below my code.

This is my code:

public static boolean isOdd(int number){
    boolean status = false;
    if(number < 0){
        status = false;
    } else if(number % 2 != 0){
        status = true;
    }
    return status;
}
public static int sumOdd(int start, int end){
    int sum = 0;
    if((end < start) || (start < 0) || (end < 0)){
        return -1;
    }
    for(int i = 0; i <= end; i++){
        if((isOdd(i))){
            sum += i;
        }
    }
    return sum;
}

The expected result is:

System.out.println(sumOdd(1, 100));// → should return 2500

System.out.println(sumOdd(-1, 100)); // →  should return -1

System.out.println(sumOdd(100, 100));// → should return 0

System.out.println(sumOdd(13, 13));// → should return 13 (This set contains one number, 13, and it is odd)

System.out.println(sumOdd(100, -100));// → should return -1

System.out.println(sumOdd(100, 1000));// → should return 247500

My result:

2500

-1

2500

49

-1

250000

2 Answers

3
Karol Dowbecki On Best Solutions

Don't declare variables at the begging of your method, only create them when they are needed. As pointed out by Jordan the problem is with for loop index, it should start from start, not 0:

public static boolean isOdd(int number) {
    if (number < 0) {
        return false;
    }
    return number % 2 != 0;
}

public static int sumOdd(int start, int end) {
    if ((end < start) || (start < 0) || (end < 0)) {
        return -1;
    }
    int sum = 0;
    for (int i = start; i < end; i++) {
      if (isOdd(i)) {
        sum += i;
      }
    }
    return sum;
}
4
Elliott Frisch On

First, your isOdd method only needs to return true if two conditions are met: the number must be greater than zero and it must be odd. That can be done with a single return and a boolean and. Like,

public static boolean isOdd(int number) {
    return number > 0 && number % 2 != 0;
}

Second, your loop should start at start (not zero). But, I would then test if start is even. If it is, increment it before the loop. Then we know we have an initial value that is odd so we no longer need to test for oddness in the loop (we can increment by two instead). Like,

public static int sumOdd(int start, int end) {
    if (end < start || start < 0 || end < 0) {
        return -1;
    }
    int sum = 0;
    if (!isOdd(start)) {
        start++;
    }
    for (int i = start; i <= end; i += 2) {
        sum += i;
    }
    return sum;
}

Which should be quite a bit more efficient.