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);
    }
    
}

Related Posts:

  • The cheapest palindrome Palindrome is a string that reads the same backward as forward, e.g., madam. You are given a string whose length is even, and each character of t… Read More
  • The colorful street There is a street by the name of colorful street in the Pretty Town. The residents of the house have decided that they will paint their houses in e… Read More
  • Teaching how to draw A man has brought a new drawing book for his child, which consists only of geometric shapes. Its consists of lessons where the child has to make dr… Read More
  • Survey Analysis WebEngage empowers companies to collect feedback and gather insights from visitors using survey and notifications. An e-commerce company used the We… Read More
  • Tom & Jerry Tom and Jerry are wonderful characters. They are always running and make us laugh. Right now, they are in a grid of houses and Jerry is running away… Read More

0 comments:

Post a Comment