codechef program #5 :Small Factorial

PROBLEM STAEMENT:
You are asked to calculate factorials of some small positive integers.
Input
An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.
Output
For each integer n given at input, display a line with the value of n!
Example
Sample input:
4
1
2
5
3
Sample output:
1
2
120
6
SOLUTION:
import java.math.BigInteger;import java.util.Scanner;public class Factorial2 {public static void main(String[] args) {int a;int[] myIntArray = new int[101];Scanner s = new Scanner(System.in);int n = s.nextInt();for (int i=0;i<n;i++){a = s.nextInt();myIntArray[i]=a;}for (int i=0;i<n;i++){String fact = factorial(myIntArray[i]);System.out.println( fact);}}public static String factorial(int n) {BigInteger fact = new BigInteger("1");for (int i = 1; i <= n; i++) {fact = fact.multiply(new BigInteger(i + ""));}return fact.toString();}}
Comments
Post a Comment