I came up with the following code to calculate the factorial of a given number:
import java.lang.*;
import java.math.*;
import java.io.*;
import java.util.*;
@SuppressWarnings("unused")
class factorial_1{
public static void main(String args[]) throws IOException
{
System.out.println("enter a number: ");
String strPhone = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
strPhone = br.readLine();
BigInteger number = new BigInteger (strPhone);
BigInteger fact = new BigInteger("1");
BigInteger i = new BigInteger("1");
BigInteger step = new BigInteger("1");
final long start = System.currentTimeMillis();
final long durationInMilliseconds = System.currentTimeMillis()-start;
for ( ; i.compareTo(number) <= 0; i=i.add(step)){
fact = fact.multiply(i);
}
System.out.println("execute Long-Running Task took " + durationInMilliseconds + "ms.");
System.out.println("the factorial of "+ number +" is "+ fact);
}
}
if you execute the code, it reads a number from the keyboard and then prints out its factorial
I exported the code as a .jar file and tried to give the number (10) as input, from the terminal
I did as this post says How to execute jar with command line arguments but nothing happened until I typed again the number
-----------Terminal-----------
roditis@NiSLab-pc2:~/Desktop$ java -jar import_number.jar 10
enter a number:
10
execute Long-Running Task took 0ms.
the factorial of 10 is 3628800
-----------Terminal-----------
Im new to linux / programming and I really look forward for your help
Thanks in advance
Roditis