diff --git a/archive/j/java/Factorial.java b/archive/j/java/Factorial.java new file mode 100644 index 000000000..bd99cc46f --- /dev/null +++ b/archive/j/java/Factorial.java @@ -0,0 +1,29 @@ +public class Factorial +{ + public static long fact(long n) + { + if (n <= 0) + return 1; + return n * fact(n - 1); + } + + public static void main(String[] args) + { + try + { + long n = Long.parseLong(args[0]); + if (n > 59) + { + System.out.println(String.format("%1$s! is out of the reasonable bounds for calculation.", n)); + System.exit(1); + } + long result = fact(n); + System.out.println(result); + } + catch (NumberFormatException e) + { + System.out.println("Usage: please input a number"); + System.exit(1); + } + } +} diff --git a/archive/j/java/README.md b/archive/j/java/README.md index 8acccc7e2..29dc602a1 100644 --- a/archive/j/java/README.md +++ b/archive/j/java/README.md @@ -6,10 +6,11 @@ Welcome to Sample Programs in Java! - [Baklava in Java](https://github.com/TheRenegadeCoder/sample-programs/issues/934) - [Hello World in Java](https://therenegadecoder.com/code/hello-world-in-java/) -- [Reverse a String in Java](https://therenegadecoder.com/code/java/reverse-a-string-in-java/) +- [Factorial in Java](https://github.com/TheRenegadeCoder/sample-programs/issues/931) +- [Fibonacci in Java](https://github.com/TheRenegadeCoder/sample-programs/issues/483) - [Fizz Buzz in Java](https://github.com/TheRenegadeCoder/sample-programs/issues/400) - [Game of Life in Java](https://github.com/jrg94/sample-programs/issues/108) -- [Fibonacci Sequence](https://github.com/TheRenegadeCoder/sample-programs/issues/483) +- [Reverse a String in Java](https://therenegadecoder.com/code/java/reverse-a-string-in-java/) ## Fun Facts