please help me with my codes, it doesnt work somehow

import java.util.Scanner;

public class calculateE
{
	public static long factorial( int n )
    {
        if( n <= 1 )     // base case
            return 1; 
        	        
        else
            return n * factorial( n - 1 );
      
    }
    
    public static void main( String [ ] args )
    {
    	Scanner input = new Scanner( System.in );
    	
        System.out.print( "Enter the terms to calculate: " ); // prompt
        int n = input.nextInt(); // input 
        
        double e = 1;
        //int t = 10;
        //int x = 7;
        for (int i = 1; i < n; i++) {
        	e = ( e + (1/(factorial( n + 1 ))) );
        }       
        
        
        if ( n < 0 )
        	System.out.print("Invalid input");

        else
        	System.out.println( factorial( n ) );
    }
}

"it doesn't work somehow" is a pretty vague description. can you be a bit more clear?
for instance: I expect ... as output, but I get ...
or: it doesn't compile and gives this error message
or: it doesn't run and gives this error message
..

1.) what is the aim of your code.
2.) your validation of n comes after you have used it (think about it) do u let a thief into your house and let him take stuff before u check him out.
3.)you calcuated e and did nothing with it(did u mean to print it?)
4.) don't show anyone this code (its awful)

Just saw your other post. you are trying to calcuate the constant e. your math is pretty bad.
e = lim (1 + 1/n)^n as n approches infinity.

I personally like Brother's formula...

sum of (2n + 2) / (2n + 1)! for 0 to n.

public class RaWR {
	
	public static double factorial(int n) { 
		double result;
		for (result = n > 0 ? n : 1; n > 1; result *= --n);
		return result;
	}
	
	public static double baseE(int n) { 
		double result;
		for (result = 0; n >= 0; result += (2 * n + 2) / factorial(2 * n -- +1));
		return result;
	}
	
	public static void main(String[] args)  { 
		System.out.println(baseE(6));
	}
	
}

Output:

2.718281828446759

This is actually the same code I gave you in your original thread...well with less eeeeeeeeeeeeeeeeeeeeeee

:P

http://www.daniweb.com/software-development/java/threads/413867

Edited 4 Years Ago by ztini: n/a

public class EulersE {
	
	/**
	 * lim (1+1/n)^n as n -> infinity
	 * @param n a very large integer
	 * @return e
	 */
	public static double e(double n){
		return Math.pow((1 + (1/n)), n);
	}
	public static void main(String[] args) {
		System.out.println(e(1e6));

	}

}
This article has been dead for over six months. Start a new discussion instead.