-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFact.java
43 lines (38 loc) · 1.06 KB
/
Fact.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
41
42
43
import java.util.Scanner;
class Fact{
/*for largest numbers
public static void main(String[] args){
int fact=1;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number to find factorial:");
int num=sc.nextInt();
if(num<0){
System.out.println("enter only positive number");
}
else if(num==0){
System.out.println("1");
}
else{
for(int i=1;i<=num;i++){
fact=num*(num-1);
}
System.out.println("value is"+fact);
}
}*/
//using functions
public static void main(String[] args){
int fact=1;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number to find factorial:");
int num=sc.nextInt();
fact=fact(num);
System.out.println("value is"+fact);
sc.close();
}
static int fact(int n){
if (n == 0)
return 1;
else
return(n * fact(n-1));
}
}