008-003-011

Recursive Method Calls

Hard

Problem Description

Recursive Method Calls

In this problem, you will implement a factorial(int n) method recursively that reads a non-negative integer n from standard input and prints its factorial.

Learning Objective: Understand recursion where a method calls itself

Overview

Implement a method that calculates factorial using recursion.

Specifications

  • Read integer n from standard input on one line
  • Implement public static int factorial(int n)
  • Return 1 if n is 0 or 1 (base case)
  • Otherwise return n * factorial(n - 1)
  • Print the result to standard output

Examples

Input Output
0 1
1 1
5 120
6 720

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