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);

## 7 Replies - 948 Views - Last Post: 06 December 2012 - 08:13 PM

### #1

# Getting the product of the first 20 odd integers.

Posted 06 December 2012 - 06:57 PM

It compiles, but not result appears.

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

### #2

## 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

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

Posted 06 December 2012 - 07:22 PM

### #4

## 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

## 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

## 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.

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

## 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

## 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

Page 1 of 1