Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #932 from TheRenegadeCoder/java-fact
Added Factorial in Java
- Loading branch information
Showing
2 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters