Results 1 to 11 of 11
  1. #1
    Basilisk3 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default 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. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    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. #3
    Basilisk3 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default 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. #4
    Basilisk3 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default 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 06:42 PM. Reason: Moderator Edit: Code tags added

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck, and again, welcome!

  6. #6
    Basilisk3 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 07:00 PM.

  8. #8
    Basilisk3 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default 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. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Basilisk3 View Post
    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. #10
    Basilisk3 is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    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. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

Similar Threads

  1. pi and the wallis' product
    By camper2 in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 10:57 PM
  2. Replies: 1
    Last Post: 03-01-2009, 05:26 PM
  3. Finished Product: What now?
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 02-11-2009, 10:41 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. sum,product,average program
    By chitwood in forum New To Java
    Replies: 7
    Last Post: 01-24-2008, 05:18 AM

Posting Permissions

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