# Thread: product of even numbers

1. Member
Join Date
Jul 2010
Posts
10
Rep Power
0

## product of even numbers

I am trying to get it to do give me the product of even numbers between 5 and 20, but I can only get it to do the odd numbers. I am new and know this should be easy, but I cannot figure it out. Any help would be great. Here is what I have:

//calculate the product of even code

public class Even
{
public static void main(String [] args)
{

int product = 1;

for ( int x = 5; x <= 20; x += 2)

product *= x;

System.out.printf("Product is %d\n", product);
}

}

2. well, the even numbers between 5 and 20 are
{6,8,10,12,14,16,18,20}

it looks like in your code you want to have the product of all these.

So, the problem here is you are starting off at 5, which when incrementing x by 2 each time, will give you {5,7,9,11,13,15,17,19}

so either start off using 6 and increment by 2, or maybe increment by one each time and test if the number is even using the modulus operator (an even number is evenly divisible by 2; if when testing a number to divide it by two if there is any remainder, it is not an even number.

As below,

Java Code:
```int product = 1;
for (int x = 5; x <= 20; x++) {
if (x % 2 == 0) {
// an even number
product *= x;
}
}```

3. Member
Join Date
Jul 2010
Posts
10
Rep Power
0

## Thanks

You Rock! I answer seems so simple. I was trying to do it using the modules and I could not get it to work. I was missing the x++. Starting with 6 though seems so simple. I guess at 2am I was not thinking straight. Thanks.

4. Member
Join Date
Jul 2010
Posts
10
Rep Power
0

## I cant get it to work for a number larger than 34

I cant get it to work for a number larger than 34. I am trying to make it work up to 50 and once I enter in 36 or higher it gives me a negative number or zero. I tried making it a double and still could not get it to work. Any idea how I can do that. Thanks

Java Code:
```public class Even
{
public static void main(String [] args)
{

int product = 1;

for ( int x = 6; x <= 20; x += 2)

product *= x;

System.out.printf("Product is: %d\n", product);
}

}```
Last edited by Fubarable; 07-05-2010 at 07:42 PM. Reason: Moderator Edit: Code tags added

5. Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Java Code:
```[cod&#101;]
// notice how the top and bottom tags are different
[/cod&#101;]```
Best of luck, and again, welcome!

6. Member
Join Date
Jul 2010
Posts
10
Rep Power
0
I tried it with product being a long but every other even number is a negative. That does not make sense to me. 44 seems to work be 42 gives me a negative number.

7. 44 doesn't work as you'll get an overflow over and beyond the negatives. You will need to use BigInteger for this, I think. When I used BigInteger, my output was like so:

Java Code:
``` 2: 2
4: 8
6: 48
8: 384
10: 3840
12: 46080
14: 645120
16: 10321920
18: 185794560
20: 3715891200
22: 81749606400
24: 1961990553600
26: 51011754393600
28: 1428329123020800
30: 42849873690624000
32: 1371195958099968000
34: 46620662575398912000
36: 1678343852714360832000
38: 63777066403145711616000
40: 2551082656125828464640000
42: 107145471557284795514880000
44: 4714400748520531002654720000```
Last edited by Fubarable; 07-05-2010 at 08:00 PM.

8. Member
Join Date
Jul 2010
Posts
10
Rep Power
0

## BigInteger

Sorry to sound so helpless but how can I get it to work with a BigInteger. I cant seem to get it to work.:confused:

9. Originally Posted by Basilisk3
Sorry to sound so helpless but how can I get it to work with a BigInteger. I cant seem to get it to work.:confused:
Sorry, but "I cant get it to work" tells us nothing about what you're doing wrong. Remember that this is your project and so we should work with your code, not mine. Have you looked at the BigInteger API? It will tell you how to construct and multiply these beasts, and that and toString are all you really need to use to get this to work.

Luck.

10. Member
Join Date
Jul 2010
Posts
10
Rep Power
0
Thanks. I am having the same issue with this on below. I have to use type long and it will work 1-20 but I need it to work from 20 - 50. Any advice on how to get it to work using long? Thanks

Java Code:
```public class Factorial
{
public static void main(String [] args)
{

System.out.println("n\tn!\n");

for ( long number = 20; number <= 50; number++ )
{
long factorial = 1;

for ( long smaller = 1; smaller <= number; smaller++ )
factorial *= smaller;

System.out.printf( "%d\t%d\n", number, factorial );
}
}
}```

11. Have you looked at this: Long.MAX_VALUE?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•