Question
Write a program that can solve the factorial problem. Your class should have two methods: The main method A static method called factorial that accepts
Write a program that can solve the factorial problem.
Your class should have two methods: The main method A static method called factorial that accepts an int as input and returns an int (the answer) The main method should: Contain a loop with a counter loops from 0 to 5 (inclusive) For each counter, call factorial. Print the counter and the result returned of the factorial. The factorial method should: Calculate the answer for the factorial. Use a loop to calculate the answer. No print statements are needed in this method, but you may want to print along the way to help you write and debug the program. Recall the rules for factorial: - The factorial of 0 is 1. Just a general rule, no multiplication needed. - The factorial of 1 is 1. Just a general rule, no multiplication needed. - The factorial of 2 is 2 * 1 = 2 - The factorial of 3 is 3 * 2 * 1 = 6 - The factorial of 4 is 4 * 3 * 2 * 1 = 24 - The factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120 Your output should simply be the following: 0! is 1 1! is 1 2! is 2 3! is 6 4! is 24 5! is 120
Factorial Problem Using Recursion
Now write a program that will solve the factorial problem, this time using recursion.
Your new class will have the same two methods. The main method will be exactly the same as it was before. The output will be the same. However, update the factorial method to no longer use a loop. Instead it should call itself.
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