Hi Please find below my code. I have run it in java 1.6. The output is absolutely right i cross verified for many digits. but i get nzec runtime error. i even handled exceptions. what is going wrong? please help!
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Main {
// Scanner in = new Scanner(System.in);
int T;
int carry = 0;
ArrayList<Integer> s = new ArrayList<Integer>();
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String args[]) {
try {
Main main = new Main();
main.T = Integer.valueOf(main.bufferRead.readLine());
for (int i = 0; i < main.T; i++) {
int j = Integer.valueOf(main.bufferRead.readLine());
main.s.add(j);
}
for (int i = 0; i < main.s.size(); i++) {
if (main.s.get(i) > 15) {
main.factorial(main.s.get(i));
} else {
int k = main.factorial2(main.s.get(i));
System.out.print(k);
}
if (i != main.s.size() - 1)
System.out.println();
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
return;
} catch (Exception e) {
// TODO Auto-generated catch block
return;
}
}
public int factorial2(int t) {
if (t == 0) {
return 1;
} else {
return t * factorial2(t - 1);
}
}
public void factorial(int t) {
ArrayList<Integer> p = new ArrayList<Integer>();
numberToArray(t, p);
for (int i = t - 1; i > 0; i--) {
multipy(i, p);
}
for (int c : p) {
System.out.print(c);
}
}
public void numberToArray(int num, ArrayList<Integer> k) {
while (num > 0) {
k.add(0, num % 10);
num = num / 10;
}
}
public void multipy(int i, ArrayList<Integer> a) {
carry = 0;
for (int j = a.size() - 1; j >= 0; j--) {
int k = i * a.get(j) + carry;
if (k > 9) {
a.set(j, k % 10);
carry = k / 10;
} else {
a.set(j, k % 10);
carry = 0;
}
}
while (carry != 0) {
a.add(0, carry % 10);
carry = carry / 10;
}
}
}