Getting the product of the first 20 odd integers.

Page 1 of 1

7 Replies - 817 Views - Last Post: 06 December 2012 - 08:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303055&amp;s=347cbcbf4b711571a2585a9035ebe3ea&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Gisengryl

Reputation: 0
• Posts: 135
• Joined: 08-September 12

Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 06:57 PM

It compiles, but not result appears.

```int counter = 1;
int a=1;
double product=0;

while (counter<=20){
if(a%2!=0){

product += a*(a+2);
}
a++;
}
System.out.println(product);
```

Is This A Good Question/Topic? 0

Replies To: Getting the product of the first 20 odd integers.

#2 nunc

Reputation: 36
• Posts: 131
• Joined: 20-November 11

Re: Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 07:04 PM

What result are you getting? Is it printing out a 0?

#3 Gisengryl

Reputation: 0
• Posts: 135
• Joined: 08-September 12

Re: Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 07:22 PM

nunc, on 06 December 2012 - 07:04 PM, said:

What result are you getting? Is it printing out a 0?

no result at all. It is blank.

#4 nunc

Reputation: 36
• Posts: 131
• Joined: 20-November 11

Re: Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 07:25 PM

Ahh silly me. You have an infinte loop. Your counter variable is never incremented, therefor your while condition is never false. Try combining your counter and "a" into a single variable, and refactoring your code to use a for loop. It should be easier to read

#5 Gisengryl

Reputation: 0
• Posts: 135
• Joined: 08-September 12

Re: Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 07:43 PM

Argh not doing well with this.. I simply cannot figure out the correct formula for this.. What I got so far.. Guess I need a break =/

```int product=1;
for (int counter =1;counter<=20;counter++){

if(counter%2!=0){
counter = counter*(counter+2);

product *= counter*(counter+2);
}
}
System.out.println(product);
```

#6 nunc

Reputation: 36
• Posts: 131
• Joined: 20-November 11

Re: Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 07:45 PM

Don't modify your counter variable inside the if statement.

Your counter in this case goes from 1 to 20. If it's odd, calculate the product. You don't do any calculations with your counter variable.

Edit: With your calculation it's like this. 1*3*5*7... So keep your previous product, and multiply the new odd number. No need to do anything fancy.

This post has been edited by nunc: 06 December 2012 - 07:47 PM

#7 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

Re: Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 08:03 PM

Try this

```public class Odd_20 {
public   static void main(String...args){
int counter = 1,cnt=1;
int a=1;
double product=1;
int i=1;
while (counter<=20*2){
if(a%2!=0){

product *=a;
}
a++;counter++;
}
System.out.println(product);

}

}
```

This post has been edited by burakaltr: 06 December 2012 - 08:33 PM

#8 burakaltr

• D.I.C Regular

Reputation: 91
• Posts: 280
• Joined: 07-November 10

Re: Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 08:13 PM

Or a fancier way of doing this
```import java.text.NumberFormat;

public class Odd_20 {

static {
System.out.println("stat");
}
public   static void main(String...args){
int counter = 1,cnt=1;
int a=1;
double product=1;
int i=1;
while (counter<=20*2){
if(a%2!=0){

product *=a;
}
a++;counter++;
}
NumberFormat form=NumberFormat.getIntegerInstance();

System.out.println(product+" =  "+form.format(product));

}

}
```

This post has been edited by burakaltr: 06 December 2012 - 08:33 PM