Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Adding odd numbers from 1 to 15

I have an exercise that wants me to write an application that calculates the product of the odd integers from 1 to 15, and displays the result.
Hereunder is the code that i have written, however I am not managing to count the odd numbers. Can someone help me please.
```public static void main(String[] args) {
int prod = 0;
int result = 0;
int div = 0;

for ( int i = 0; i != 16; i++ ) {

if (( i % 2 ) != 0)  {

prod = ( i * i );
result = (prod + prod);

System.out.println(prod);

}
}
}
}```

2. ## Re: Adding odd numbers from 1 to 15

however I am not managing to count the odd numbers.
Can you show what the program prints out and add some comments explaining what is wrong with what is printed and show what you want to be printed?

What is the variable: result used for?

Can you write an equation that shows how to get the product of the first 4 odd numbers?

3. ## Re: Adding odd numbers from 1 to 15

did the exercise myself for practice
how do i wrap this in java code?

ok, so I read your code, and I think you should think it out on paper before even writing the code. Figure out how you wanna do it step by step then try to translate that into code, cause your code seems really off target.

so your code begins when i iterates to 1. Once i = 1, since 1%2 != 0, you do product = 1*1, after that you say result = 1 + 1 which is 2, then you print out product which is 1. So if you're not going to use result, what's the point of having it there in the first place?

>>>>>> Code removed

4. ## Re: Adding odd numbers from 1 to 15

The Problem with Spoon-feeding