#C12791. Factorial Calculator
Factorial Calculator
Factorial Calculator
In this problem, you are required to compute the factorial of a non-negative integer n using one of two methods: iterative or recursive.
The factorial of a number is defined as:
\( n! = \begin{cases} 1, & n = 0 \\ n \times (n-1)!, & n \ge 1 \end{cases}\)
You will be given an integer n and a string indicating the method. Your task is to calculate \( n! \) using the specified method. If the input integer is negative, or if the method is not "iterative" or "recursive", then output the corresponding error message.
Note: The input is read from standard input and the output should be written to standard output.
inputFormat
The input consists of a single line containing an integer n and a method string separated by whitespace.
Constraints: n is a non-negative integer and the method is either "iterative" or "recursive".
outputFormat
Output the factorial of n computed using the specified method. If the input is invalid, output the corresponding error message exactly as described below:
- If n is negative or not an integer, output: "Input must be a non-negative integer."
- If the method is neither "iterative" nor "recursive", output: "Method must be 'iterative' or 'recursive'."
0 iterative
1