I am writing a basic program that takes a parameter and then calculates the product of the odd numbers based on that parameter.
It works if I pass anything from 1 up to 19 but at 20 my output is a negative number and at 35 it is 0??
I'm sure there is something wrong in the algorithm?
Suggestions:
import java.util.ArrayList;
import java.util.List;
public class ProductOfIntegers {
public static void productOfOddIntegers(int i){
int[] numbers = new int[i];
for(int j = 0; j < numbers.length; j++){
numbers[j] = j + 1;
}
List<Integer> oddNumbers = new ArrayList<Integer>();
for(int j = 0; j < numbers.length; j++){
if(numbers[j] % 2 != 0){
oddNumbers.add(numbers[j]);
}
}
int product = 1;
for(int n: oddNumbers)
product*=n;
System.out.println(oddNumbers);
System.out.println(product);
}
public static void main(String [] args){
productOfOddIntegers(15);
}
}