-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.java
More file actions
39 lines (27 loc) · 880 Bytes
/
recursion.java
File metadata and controls
39 lines (27 loc) · 880 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
import java.util.Scanner;
public class recursion {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Factorial Generator");
System.out.print("Enter the Number : ");
int num = input.nextInt();
long result = factorialMethod(num);
System.out.println("the Factorial is " + result);
long myResult = factorialThroughLoop(num);
System.out.print("The Factorial is by loop " + myResult);
}
// Recursion
public static long factorialMethod(int num){
if(num == 1){
return 1;
}
return num * factorialMethod(num-1);
}
public static long factorialThroughLoop(int num){
int result = 1;
for(int i = 1; i <= num ; i++){
result *= i;
}
return result;
}
}