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  633 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
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
#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
#5
Re: Getting the product of the first 20 odd integers.
Posted 06 December 2012  07:43 PM
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
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
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
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
