008-006-006
Recursive Method: Factorial Calculation
Medium
Problem Description
Recursive Method: Factorial Calculation
In this problem, you will create a program that calculates the factorial of an integer n using a recursive method and prints the result.
Learning Objective: Understand the basics of recursive processing where a method calls itself
Overview
Create a method that calculates factorial using recursion. Factorial is the product of integers from 1 to that number (e.g., 5! = 5 × 4 × 3 × 2 × 1 = 120).
Specifications
- Define
factorial(int n)method in theMathclass - Receive int parameter n and return n! as int
- Use recursion (return 1 if n <= 1, otherwise n × factorial(n-1))
- Read one integer n from standard input and print the result of
factorial(n)
Input/Output
- Input: Integer n (one line)
- Output: The value of n!
Example
Input: 5
Output: 120
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up