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
Step by step descriptive logic to find sum of odd numbers between 1 to n. Input upper limit to find sum of odd numbers from user. Store it in some variable say N. Initialize other variable to store sum say sum = 0. To find sum of odd numbers we must iterate through all odd numbers between 1 to n.
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.
So, without further ado, let’s begin this tutorial. How Does This Program Work ? Enter the maximum value: 22 Odd Numbers Between 0 To 22 are: 1 3 5 7 9 11 13 15 17 19 21 The Sum of Odd Numbers From 0 To 22 is 121.
An odd number is a number that is not divisible by 2. The remainder in the case of an odd number is always 1. Example: 1, 3, 5, 7, 9, etc. How to check if a number is odd or not? If a number is not divisible by 2 then it is an odd number.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With