-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
33 lines (32 loc) · 962 Bytes
/
Factorial.java
File metadata and controls
33 lines (32 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Program: Calculate Factorial of a Number Using Recursion
// Topic: Recursion and Mathematical Computation
// Description: Reads an integer from user input and calculates its factorial using a recursive function `fact()`.
// Demonstrates base and recursive cases in recursion, where the factorial of a number is computed as `n * fact(n-1)`
// until the base case (n == 0 or n == 1) is reached.
package recursion;
import java.util.*;
/**
*
* @author Samim
*/
public class Factorial
{
public static int fact(int n){
if(n==0){
return 1;
}
else if(n==1){
return n;
}
else{
return n*fact(n-1);
}
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter Number :");
n=sc.nextInt();
System.out.println(fact(n));
}
}