-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path24.2 Recursion Method Factorial.java
More file actions
47 lines (34 loc) · 983 Bytes
/
24.2 Recursion Method Factorial.java
File metadata and controls
47 lines (34 loc) · 983 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Create a method named ‘factorial’ in your program that will display the factorial of any given number. factorial of an integer is the product of numbers ranging from 1 to N
Input Format
A single interger that represents the number whose factorial you need to find
Constraints
1<=N<=10
Output Format
Displays the output of number N
Sample Input 0
2
Sample Output 0
2
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
//recursive function
static int factorial (int n)
{
if(n<=1)
return 1;
return (n * factorial(n-1));
}
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner (System.in);
int n = sc.nextInt();
int res = factorial(n);
System.out.print(res);
}
}