7 Replies - 571 Views - Last Post: 06 December 2012 - 08:13 PM Rate Topic: -----

#1 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • 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. :whatsthat:

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  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 08-September 12

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

Posted 06 December 2012 - 07:22 PM

View Postnunc, 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.
Was This Post Helpful? 0
  • +
  • -

#4 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#5 Gisengryl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • 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);

Was This Post Helpful? -1
  • +
  • -

#6 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 92
  • View blog
  • Posts: 276
  • 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

Was This Post Helpful? 1
  • +
  • -

#8 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 92
  • View blog
  • Posts: 276
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1