Question
Ch 4B Program 1 OL: Factorial.java Please adhere to the Standards for Programming Assignments and the Java Coding Guidelines. Create a program that computes the
Ch 4B Program 1 OL: Factorial.java
Please adhere to the Standards for Programming Assignmentsand the Java Coding Guidelines.
Create a program that computes the factorial of a number that is input from the user.
A factorial is the product of all the numbers up to the number entered.
For example, 6-factorial is 720 because 6*5*4*3*2*1 is 720.
Six-factorial is written as 6!
Keeps prompting the user for values to calculate the factorial UNTIL a 0 is entered by the user.
If a 0 is entered, then quit the program by saying Goodbye. Here 0 is acting as a sentinel value that terminates the loop.
Do not accept numbers greater than 10 or less than 0.( You need to allow the user to enter 0 and once the user does the program terminates without calculating or displaying factorial.
- All code must reside in the main() method
- Do not use any concept that is not covered in class.
- Absolutely no loop with (true ) as the condition [e.g while (true)] and break statement
Include output from this Test Data: 22
7
1
0
IPO and Algorithm to get you started.
Input: An integer within 0-10
Processing:
- Validate the input
- Calculating the factorial of the number if not 0. The formula for calculating the factorial of n is
N! = n *(n -1) * (n-2).....* 1
4! = 4 * 3 *2 *1
Output: The factorial.
Algorithm:
Declare and initialize the variables
Remember the 0 input is used to terminate the program, so should allow the user to enter a 0.
Start the do loop
Prompt the user to enter a number between 0 and 10.
Remember the 0 input is used to terminate the program, so should allow the user to enter a 0.
Validate the input using a while loop
while the number is outside 0 and 10
Prompt for the number again
Read the number
End while
Once we are here it means that the input is a valid input including 0.
if the number is not zero only then we must calculate the factorial otherwise skip.
This is a good place to use an if --else statement.
If not zero then calculate the factorial and display it
Otherwise display the goodbye message
End the top do while (think about the sentinel to terminate) ;
Example Output : *******Factorial********* Please enter a value between 1-10 (0 to quit): 4 4! = 24 *******Factorial********* Please enter a value between 1-10 (0 to quit):24 Value is out of range. Please Re-enter a value between 1-10 (0 to quit): 6 6! = 720 *******Factorial********* Please enter a value between 1-10 (0 to quit): 0 Goodbye!! |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started