Monday, 17 August 2015

Small Factorials Problem

Standard
You are asked to calculate factorials of some small positive integers.
Input
An integer T, denoting the number of testcases, followed by T lines, each containing a single integer N.
Output
For each integer N given at input, output a single line the value of N!
Input Constraint
1 <= T <= 100
1 <= N <= 100
 


Sample Input

4
1
2
5
3

Sample Output

1
2
120
6
 
 
Code:

import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestClass8 {

    public static void main(String[] args) {
        try{
        BufferedReader br =
          new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        int N = Integer.parseInt(line);
        for (int i = 0; i < N; i++) {
        String liner = br.readLine();
        int NS = Integer.parseInt(liner);        
        calculateFactorial(NS);
        }
    }catch(Exception e){}
    }
    public static void calculateFactorial(int n) {
        
        BigInteger result = BigInteger.ONE;
        for (int i=1; i<=n; i++) {
            result = result.multiply(BigInteger.valueOf(i));
        }
        System.out.println(result);
    }
    
}

0 comments:

Post a Comment