# Help please, stuck on for statement problem.

• 10-30-2009, 04:39 AM
shroomiin
Help please, stuck on for statement problem.
Hey everyone. i have been trying to figure this out for quite some time now and i am just stuck on using the for loop. i need to write an application that determines the value for mathematical constant e by using the following formula . allow the user to enter the number of terms to calculate..

e=1+1/1!+1/2!+1/3!...

i am just stuck on this, i know i need to first make a method that calculates n! which i am able to do, this code works..

Code:

```import java.util.Scanner; public class Factorials {         public static void main(String[] args) {                                 Scanner input = new Scanner(System.in);                                 int num;                 int nFact;                                 System.out.println("Please enter a number: ");                 num=input.nextInt();                                 nFact = fact(num);                                        System.out.println(num + "! = " + nFact + ".");                }         static int fact(int num){                 if(num<=1)                         return 1;                                 else{                 return num*fact(num-1);         }                         } }```
i dont know how or where to put the for loop. anybody who cares to clear this up for me is appreciated. thanks!
• 10-30-2009, 05:07 AM
travishein
This sample now asks you for the value of n of which to iterate up to. The compute e function does the for loop of invoking 1/n! and summing it with the previous terms.

Likely didn't need to make the factorial function into a loop to be non-recursive as well, but this way should use less heapspace for larger N.

Code:

```import java.util.Scanner; public class Factorial {   public static void main(String[] args) {     Scanner input = new Scanner(System.in); // that original just compute n! //    System.out.println("Please enter a number: "); //    in num = input.nextInt(); //    int nFact = fact(num); //    System.out.println(num + "! = " + nFact + ".");           // now compute e= 1/1! + 1/2! + ...         System.out.prints("Enter n for e=1/1! + ... + 1/n! :");     int n = input.nextInt();         double e = computeE(n);                                 System.out.println("e=" + e);   }         /**         * computes e = 1/1! + ... + 1/n!         */   public static double computeE(int n) {                 double result = 0;                 for (int i = 1; i <=n; i++) {                         double term = (double)1 / fact(i);                         result += term;                 }                 return result;         }                   /**   * computes n factorial using a for loop (not recursive method call)   * @param num   * @return   */   static int fact(int num) {     int result = num;     for (int i = num-1; i > 0; i --) {       int newResult = result * i;        // could do result *= i, but do this additional variable for debug output clarity.       //System.out.println("doing " + result + " * " + i + " = " + newResult);       result = newResult;     }     return result;   } }```
• 10-30-2009, 08:58 AM
JosAH
There is no need to compute those factorials over and over again. Suppose you have the value for the n-th term == 1/n!. If you want to know the n+1st term (which is 1/(n+1)!) simply do this: (value of n-th term)/(n+1) == 1/(n!*(n+1)) == 1/(n+1)!. In code:

Code:

```double e= 1; double term= 1; for (int n= 1; n <= N; n++) {   term/= n;   e+= term; } System.out.println("e: "+e);```
kind regards,

Jos
• 10-30-2009, 02:27 PM
shroomiin
thanks so much for the help!