Factorial.java
import java.util.*;
import java.lang.*;
public class Factorial {
public static void main(String[] args)
{
if (args.length == 0)
System.out.println("Example: java Factorial 10");
try
{
for (int i=0; i < args.length; i++)
{
System.out.print("Fact of Param # " + (i+1) + ": " + Fact(args[i]));
}
}
catch(Exception E)
{
System.out.println(E.getMessage());
}
}
public static long Fact(String f) throws Exception
{
int f1 = Integer.parseInt(f);
long Result = 1;
if (f1 < 0)
throw new Exception("I can not calculate this!");
if (f1 > 20)
throw new Exception("that is a big ####");
try
{
for (int j=1; j <= f1; j++)
Result *= j;
}
catch(Exception e)
{
System.out.println(e.getMessage());
throw e;
}
return Result;
}
}
Thursday, February 28, 2008
Sample Code On Factorial
Labels:
programming
Subscribe to:
Post Comments (Atom)
Archives
-
▼
2008
(35)
-
▼
February
(35)
- How To Make http To https Request
- User With Password Never Expires on MS-Adam
- Sample Code On Use XML in Java to Write Save Docum...
- Sample Code On Random Sentence Generator
- Sample Code On How To Send Email Using Java Mail
- Keeping a Standard Coding Template
- Sample Code On How to Upload a File and How to Sto...
- Sample Code On Example Uses Of Calendar Class
- Sample Code On How to Get the Start Date of the We...
- How to Make a Java Class Immutable
- Simple Program for Reading Writing Content From File
- Code for Java-Mail
- Sample Code On Simple Date Validation
- Sample Code On Cows and Bulls Game
- Sample Code On BasicDateFormatting.java
- Sample Code On TTSTest.java
- Sample Code On Process Execution
- Sample Code On Read Contents Of URL
- Sample Code On XMLParserUtils
- Sample Code On Bin.java
- Sample Code On ASCII_code
- Sample Code On Prime
- Sample Code On Factorial
- A Serializable JButton
- A Java Calculator
- The example draws several moving wadges on a circle
- Demonstrate Boolean Values
- StackDemo shows how to use the linear set of objects
- Hide Password From Command Line
- Input through Keyboard
- Assertion in Java
- String in Java
- Variable and Expressions
- A Simple HelloWorld.java program
- History of Java
-
▼
February
(35)
No comments:
Post a Comment