Purchase Solution

Writing a Program in Java

Not what you're looking for?

Ask Custom Question

(The following is exactly how the problem is presented)

import java.util.Scanner;
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) {
// Make a Scanner to read data from the console
Scanner console = new Scanner(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.nextLine();
while ((xString != null) && (xString.length() > 0)) {
double x = Double.parseDouble(xString);

// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------

// --------------------------------
// --------- END USER CODE --------
// --------------------------------

// Start over again
System.out.println(
"Enter a value for x (or a blank line to quit):");
xString = console.nextLine();
}
}

}
The value ex can be approximated by the sum:
1 + x + x2/2! + x3/3! + ... + xn/n!
Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 10, 50, and 100. Your program should repeat the calculation for new values of x until the user says she or he is through. The expression n! is called the factorial of n and is defined as
n! = 1 * 2 * 3 * ... * n
Define variables for the value of n! in the current iteration of the loop. Within the loop, you can then simply update this variable to be equal to the previous value multiplied by the value of the loop iterator. For example, if the loop iterator variable is n and the current factorial value is in variable fact, the following line will update the value of the factorial:
fact = fact * n;
The program should use a DecimalFormat object (local variable nFormat) to format the output. See Section 2.1 for more information on the DecimalFormat class.

Make sure that you use a variables of type double for the calculations in this project

Purchase this Solution

Solution Summary

This solution provides help with writing a program in Java.

Purchase this Solution


Free BrainMass Quizzes
Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

C# variables and classes

This quiz contains questions about C# classes and variables.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.