Skip to content

Commit 4a5729c

Browse files
authored
Merge pull request #176 from Buddhimah/master
Java_Factorial_Program
2 parents 0d8e345 + 066b898 commit 4a5729c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

factorial/Factorial.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Scanner;
2+
/**
3+
* Created by buddhimah on 10/16/2017.
4+
*/
5+
public class Factorial {
6+
public static int fac(int number){
7+
int r;
8+
if(number == 1){
9+
return 1;
10+
}
11+
r = fac(number - 1)*number;
12+
return r;
13+
}
14+
public static void main(String args[]){
15+
Scanner sc = new Scanner(System.in);
16+
System.out.println("Enter a number: ");
17+
int num =sc.nextInt();
18+
if(num < 0){
19+
Error a = new Error("factorial for negative numbers is undefined");
20+
System.out.println(a.getMessage());
21+
Factorial.main(new String[1]);
22+
}else {
23+
System.out.println(fac(num));
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)