0% found this document useful (0 votes)
9 views

Program 14 FACTORIAL

Uploaded by

aliasgarop123
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 views

Program 14 FACTORIAL

Uploaded by

aliasgarop123
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/ 4

Program14:FACTORIAL(REC

URSION)______________________________________________
import java.util.Scanner;

public class FactorialRecursion {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

int result = factorial(num);


System.out.println("Factorial of " + num + " is: " +
result);
}

// Recursive method to calculate factorial


private static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}}

AlGORITHM
_____________________________________________

 Step 1: Input the integer n.


 Step 2: Check if n is equal to 0 or 1.
 Step 3:
 If n is equal to 0 or 1, then return 1.
 Else go to Step 4.
 Step 4: Return n * factorial(n - 1).
 Step 5: Output the factorial of n.
OUTPUT
_____________________________________________
Variable Table
_____________________________________________

You might also like