Archive for the ‘Java’ Category

“e” Calculator

Thursday, February 7th, 2002

[code lang=”java”]
/*——————–
“e” Calculator
Calculates the value of the number e
Carlos Macasaet
February 7, 2002
——————–*/

import java.io.*;

class calc_e {
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.print(”Input number of terms to evaluate: “);
System.out.flush();

String inputLine = stdin.readLine();

if(inputLine != null) {
int numterms = Integer.parseInt(inputLine);
System.out.println(”e = ” + Calc_e(numterms - 1));
} else System.out.println(”No input!”);

System.out.println(”Program terminated.”);
}

static double Factorial(int n) {
return (n == 0) ? 1 : (n * Factorial(n - 1));
}

static double Calc_e(int term) {
return (term == 0) ? 1 : Calc_e(term - 1) + (1 / Factorial(term));
}
}
[/code]


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported