Skip to content

Commit

Permalink
Merge pull request #932 from TheRenegadeCoder/java-fact
Browse files Browse the repository at this point in the history
Added Factorial in Java
  • Loading branch information
jrg94 committed Dec 31, 2018
2 parents b474a91 + 6bcf0df commit 6d412d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
29 changes: 29 additions & 0 deletions 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);
}
}
}
5 changes: 3 additions & 2 deletions archive/j/java/README.md
Expand Up @@ -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

Expand Down

0 comments on commit 6d412d0

Please sign in to comment.