Results 1 to 4 of 4
 10302009, 04:39 AM #1Member
 Join Date
 Sep 2009
 Posts
 31
 Rep Power
 0
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..
Java 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(num1); } } }
 10302009, 05:07 AM #2
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 nonrecursive as well, but this way should use less heapspace for larger N.
Java 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 = num1; 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; } }
 10302009, 08:58 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,074
 Blog Entries
 7
 Rep Power
 24
There is no need to compute those factorials over and over again. Suppose you have the value for the nth term == 1/n!. If you want to know the n+1st term (which is 1/(n+1)!) simply do this: (value of nth term)/(n+1) == 1/(n!*(n+1)) == 1/(n+1)!. In code:
Java Code:double e= 1; double term= 1; for (int n= 1; n <= N; n++) { term/= n; e+= term; } System.out.println("e: "+e);
Jos
 10302009, 02:27 PM #4Member
 Join Date
 Sep 2009
 Posts
 31
 Rep Power
 0
Similar Threads

[SOLVED] Problem with an If Statement.
By ChrisMayhew in forum New To JavaReplies: 8Last Post: 01082009, 04:58 AM 
problem while using return statement
By shaluchandran in forum New To JavaReplies: 10Last Post: 12122008, 07:29 PM 
IfElse statement problem
By MomenT in forum New To JavaReplies: 9Last Post: 10232008, 08:06 AM 
ForLoop statement problem
By MomenT in forum New To JavaReplies: 4Last Post: 10012008, 01:01 PM 
Please help me with this Problem Statement
By KMS in forum New To JavaReplies: 1Last Post: 09222008, 08:44 PM
Bookmarks