Question

In: Computer Science

In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of...

In mathematics, the notation n! represents the factorial of the nonnegative integer n. The factorial of n is the product of non-negative numbers from 1 to n.

Design a program that asks the user to enter a nonnegative integer and then displays the factorial of that number.

  • Module main. Asks the user to enter a non-negative integer. A loop is used to require user input until a nonnegative number is entered. Once a nonnegative number is entered, the integer is passed to Module factorial.

  • Module factorial. Accepts a non-negative integer from Module main and uses a loop to calculate the factorial of that number. The number resulting from each step of the calculation will be written to an output file products.dat. The following numbers would be written to the output file for 7!

42
210
840
2520
5040

Module readFactorial. Changes the status of products.dat to an input file. Reads and displays all numbers in the file. The final number should be labeled as the solution to the problem.

Expected Input/Output

Your results should be similar to the following:

Please enter a non-negative integer: -5

The number must be non-negative.

Please enter a non-negative integer: 0

The number must be non-negative.

Please enter a non-negative integer. 7

Following are intermediary calculations for 7 factorial:

42
210
840
2520
5040

7 factorial is 5040.

Please use java to generate the program. And must have these modules and use these guidelines!!!

Solutions

Expert Solution

Program Code to Copy

import java.io.*; import java.util.Scanner; class Main{ static void module(int n) throws IOException { int ans = n; //Writer to write to file BufferedWriter bw = new BufferedWriter(new FileWriter("products.dat")); for(int i=n-1;i>1;i--){ //Multiply with current number ans = ans*i; //Write to file bw.write(ans+"\n"); } //Flush and close file bw.flush(); bw.close(); } public static void main(String[] args) throws IOException { //Prompt for non negative number System.out.print("Enter a non negative number: "); Scanner obj = new Scanner(System.in); int n = obj.nextInt(); //Call module and store the info in file module(n); //Read the file BufferedReader br = new BufferedReader(new FileReader("products.dat")); String s; String finalAns=""; //Read all lines while((s=br.readLine())!=null){ System.out.println(s); finalAns = s; } //Write factorial to output System.out.println(n+" factorial is "+finalAns); br.close(); } }

Screenshot:

Output:.


Related Solutions

The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined...
The factorial of a nonnegative integer n is written n! (Pronounced “n factorial”) and is defined as follows: n! = n.(n-1). (n-2).……...1 (for values of n greater than 1) n!=1 (for n = 0 or 1) For instance, 5! = 5.4.3.2.1 which is 120. Write a Python program that reads a nonnegative integer n and calculates and prints its factorial. Your program should display a suitable error message if n entered as a negative integer.    Figure   6.   Exercise   6  ...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2* 1 = 120 3! = 3 * 2 * 1 = 6 2! = 2 * 1 = 2 The value of 0! is 1. Write a program that asks user to enter an integer > 0; if a valid value is...
For any nonnegative integer n, let Y1 < Y2 < · · · < Y2n+1 be...
For any nonnegative integer n, let Y1 < Y2 < · · · < Y2n+1 be the ordered statistics of 2n + 1 independent observations from the uniform distribution on [−2, 2]. (i) Find the p.d.f. for Y1 and the Y2n+1. (ii) Calculate E(Yn+1). Use your intuition to justify your answer.
Let f1(n) and f2(n) be asymptotically nonnegative functions. Using the formal definition of Θ-notation, prove that...
Let f1(n) and f2(n) be asymptotically nonnegative functions. Using the formal definition of Θ-notation, prove that max(f1(n), f2(n)) = Θ(f1(n)+f2(n)).
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined...
In C++ Let n be a non-negative integer. The factorial of n, written n!, is defined by 0 ! 5 1, n! = 1·2·3· · ·n if n $ 1. For example, 4! = 1·2·3·4 = 24. Write a program that prompts the user to enter a nonnegative integer and outputs the factorial of the number. Allow the user to repeat the program. Example: If the user enters a 3 then your program should perform answer = 3 * 2...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
1. Prove or Disprove: If n is a nonnegative integer, then 5 | (2*4n + 3*9n)
1. Prove or Disprove: If n is a nonnegative integer, then 5 | (2*4n + 3*9n)
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 ×...
The factorial of a non-negative integer is defined as follows: n! = 1 × 2 × ... × (n − 1) × n A. Write a function that takes an input value n and returns its factorial result n!. Ensure that your function returns an error statement if the input n is either a negative or a non-integer value. You cannot use the prod() or factorial() functions. The Euler number e is calculated as the sum of the infinite series...
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:  Enter an integer: -5 ERROR!!! Tactorial of a negative number doesn't exist  Enter an integer: 10  Factorial = 3628800
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT