forked from dharmanshu1921/porject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursion07.java
40 lines (32 loc) · 926 Bytes
/
recursion07.java
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
34
35
36
37
38
39
40
// Java Program to Find Factorial of a Number
// where N>=0 is currently N>1
// Importing input output classes
import java.io.*;
// importing utility classes
import java.util.*;
// Main class
class Recursion {
// Method 1
// To calculate factorial
static int factorial(int n)
{
// Handling base case
// iIf value of n=1 or n=0, it returns 1
if (n == 0 || n == 1)
return 1;
// Generic case
// Otherwise we do n*(n-1)!
return n * factorial(n - 1);
}
// Method 2
// main driver method
public static void main(String[] args)
{
// Calling method 1 to compute factorial and
// storing the result into a variable
int ans = factorial(5);
// Print and display the factorial of number
// customly passed as an argument
System.out.println("Factorial of 5 is :" + ans);
}
}