0% found this document useful (0 votes)
9 views1 page

Linear Recurs I On

The document describes a Java program that uses recursion to calculate the factorial of a user-input number by calling a fact method that recursively multiplies the input number by the factorial of one less than the input number until the base case of the input being 0 is reached.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Linear Recurs I On

The document describes a Java program that uses recursion to calculate the factorial of a user-input number by calling a fact method that recursively multiplies the input number by the factorial of one less than the input number until the base case of the input being 0 is reached.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

package algorithm;

import java.util.Scanner;
public class LinearRecursion {

5 public static void main(String args[]){


Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number to compute the factorial: ");
int num = scanner.nextInt();
int factorial = fact(num);
10 System.out.println("The factorial of " + num + " is " +factorial +
".");
}
static int fact(int n)
{
15 int output;
if( n==0 ){
return 1;
}
output = fact(n-1)* n;
20 return output;

}
}

25

You might also like