Results 1 to 7 of 7

Thread: Help with loop

  1. #1
    lifelesschild is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Question Help with loop

    The question is:
    You can approximate e by using the following series
    1+1/1!+1/2!+1/3!+1/4!+⋯
    Write a program that finds out how many TERMS of this series you need before you first get 2.71828183 approximately.

    I am having troubles writing my actual program. I am trying to loop and test then count until i hit the number but i feel as if my program is not what the question is asking. I get that the question is asking how many terms are needed to find the approximation of e and not the number given because the actual approximation is 2.71828182845904523, which is precise.

    Below is my program. I would really appreciate some feedback.

    Java Code:
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		double sum=1;
    		double k=1;
    		while (k<=10)
    		{
    			sum=sum+(1/factorial (k));
    			k++;
    			if (sum>=2.71828183)
    				break;
    		}
    		double term=k-1;
    	JOptionPane.showMessageDialog (null,"You need "+term+" terms before you first get 2.71828183 approximately. ");
    	}
    	
    	public static double
    	factorial (double k) {
    		double fact=1;
    		for (double j=1;j<=k;j++)
    			fact=fact*j;
    		return fact;
    	}
    
    }
    Last edited by pbrockway2; 03-07-2012 at 04:42 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with loop

    Assuming that to "first get 2.71828183" means that you first obtain a value between 2.718281825 and 2.718281835 then that is what you should test for. (With a bit of thinking you might be able to simplify the test: but those are the values between which numbers round to 2.71828183.)

  3. #3
    lifelesschild is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help with loop

    how would you obtain the value between 2.718281825 and 2.718281835?

  4. #4
    lifelesschild is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help with loop

    when i compile and run it, it says 10 terms are needed

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with loop

    Quote Originally Posted by lifelesschild View Post
    how would you obtain the value between 2.718281825 and 2.718281835?
    I'm not sure I understand this. You obtain the value by adding the terms of the series together. The point is to understand when (after how many terms) you reach a value within these limits.

  6. #6
    berb12 is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

    Default Re: Help with loop

    Your while loop stops at when k=10, don't you want it to stop when (sum<e)?

  7. #7
    berb12 is offline Member
    Join Date
    Nov 2011
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  3. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  4. Replies: 5
    Last Post: 04-30-2010, 06:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM

Posting Permissions

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