3.04
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.text.DecimalFormat;
/**
Iteratively computes e^x for a given value x, outputing values at iteration
1 to 10, 50, and 100
*/
public class Ex {
public static void main(String[] args) throws IOException {
// Make a BufferedReader to read data from the console
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in));
// Make a NumberFormat object that we'll use when printing values
// of n in the loop below
DecimalFormat nFormat = new DecimalFormat("000");
// Read in a number for x
System.out.println("Enter a value for x (or a blank line to quit):");
String xString = console.readLine();
while ((xString != null) && (xString.length() > 0)) {
double x = Double.parseDouble(xString);
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
double down =1,up =1 ,sum =0,i;
System.out.println("Enter a value for n :");
String nString = console.readLine();
double n = Double.parseDouble(nString);
for ( i=1;i<=n ;i++)
{
down=down*i ;
up=up*x ;
sum = sum+up/down ;
}
System.out.println( "for x=" + x + " n=" + n + " the sum is " + (sum+1));
}
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
// Start over again
System.out.println(
"Enter a value for x (or a blank line to quit):");
xString = console.readLine();
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.text.DecimalFormat;
/**
Iteratively computes e^x for a given value x, outputing values at iteration
1 to 10, 50, and 100
*/
public class Ex {
public static void main(String[] args) throws IOException {
// Make a BufferedReader to read data from the console
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in));
// Make a NumberFormat object that we'll use when printing values
// of n in the loop below
DecimalFormat nFormat = new DecimalFormat("000");
// Read in a number for x
System.out.println("Enter a value for x (or a blank line to quit):");
String xString = console.readLine();
while ((xString != null) && (xString.length() > 0)) {
double x = Double.parseDouble(xString);
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
double down =1,up =1 ,sum =0,i;
System.out.println("Enter a value for n :");
String nString = console.readLine();
double n = Double.parseDouble(nString);
for ( i=1;i<=n ;i++)
{
down=down*i ;
up=up*x ;
sum = sum+up/down ;
}
System.out.println( "for x=" + x + " n=" + n + " the sum is " + (sum+1));
}
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
// Start over again
System.out.println(
"Enter a value for x (or a blank line to quit):");
xString = console.readLine();
}
}