Results 1 to 3 of 3
  1. #1
    kmjt is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default Stuck on a probably simple question.

    I'm stuck on the following question:

    The value of the mathematical constant e, which is 2.718281828459045, can be approximated as an infinite series as follow:
    e = 1 + 1/1! + 1/2! + 1/3! + ...., where n! = 1󫎿4....譶.
    Write a program that approximate e. Your program should keep adding terms until the value of the current term becomes less than a small floating-point number entered by the user. Test your program using the following small floating-point numbers.
    0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001, 0.00000001, 0.000000001, 0.0000000001, and 0.00000000001.
    You should print the approximated value of e, as well as the number of terms required to generate this value.
    This is what I have so far:

    Java Code:
    import java.util.Scanner;
    
    public class MathConstant 
    
    {
    	
    	public static void main(String[] args)
    
    	{
    
    		Scanner input = new Scanner(System.in);
    		
    		
    		System.out.println("Enter an integer value: ");
    		int value = input.nextInt(); // Getting the value from user input
    		double number = 1;
    		double total = 0;
    		
    		while (number < value)			
    		
    			{
    			
    			number += 1;
    			total ++  
    			
    			}
    		
    
    		
    	}
    
    }
    All I really need help with is how to I incorporate a factorial in a loop? How would I do like 400! without writing the whole thing out?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Google

    It will provide you an explanation of how to calculate factorial. Or if you are lazy I'm sure google can even turn up the code.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

Similar Threads

  1. Simple Question
    By stackptr89 in forum New To Java
    Replies: 13
    Last Post: 01-29-2011, 05:35 PM
  2. First Assignment and already stuck....Newbie Question
    By Danieldcc in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 11:44 PM
  3. very simple Question
    By arsenal4ever_11 in forum NetBeans
    Replies: 2
    Last Post: 05-27-2010, 08:51 PM
  4. some simple question?
    By jperson in forum New To Java
    Replies: 4
    Last Post: 05-03-2010, 05:32 PM
  5. Stuck - simple program
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-24-2008, 07:44 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
  •