Hi im trying to calculate the constant e, this is my codes, the program doesnt output correct value, im new to java, please help

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( e );
    }
}

Recommended Answers

All 6 Replies

Shouldn't

for (int i = 1; i < n; i++) {
        	e = ( e + (1/(factorial( n + 1 ))) );
        }

be

for (int i = 1; i < n; i++) {
        	e = ( e + (1/(factorial( i + 1 ))) );
        }

thanks for reply but it still produces incorrect answer :(

I didn't say it would. Just that the expression inside the loop was incorrect! :-) You need to analyze the code line by line to be sure it is doing what you think it is!

Member Avatar for ztini

you need more base e:

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

}

Output:

2.718281828446759

sorry but i dont get it. can you fix my codes? pleaseee !!

EEEEEEEEEEE! Arrrrrgh! ROFLMAO!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.