-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
63 lines (60 loc) · 1.45 KB
/
Factorial.java
File metadata and controls
63 lines (60 loc) · 1.45 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
//factorial with do-while loop
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args) {
int i=1,fact=1;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
do{
fact=fact*i;
i++;
}while(i<=n);
System.out.println(fact);
}
}
//factorial with while loop
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int i=1,fact=1;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number to get factorial:");
int n=sc.nextInt();
while(i<=n){
fact=fact*i;
i++;
}
System.out.println("The Factorial of "+n+" is:"+fact);
}
}
//factorial with for loop
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int fact=1;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number to get factorial:");
int n=sc.nextInt();
for (int i=1;i<=n;i++){
fact=fact*i;
}
System.out.println("The Factorial of "+n+" is:"+fact);
}
}
*/
//Recursive Approach
public class Factorial{
static int fact(int n){
if (n==0)
return 1;
return n*fact(n-1);
}
public static void main(String[] args) {
int k=1;
System.out.println(fact(k));
}
}