


You are given an integer ‘N’. You have to print the value of Factorial of ‘N’. The Factorial of a number ‘N’ is defined as the product of all numbers from 1 to ‘N’.
For Example:Consider if ‘N’ = 4, the Factorial of 4 will be the product of all numbers from 1 to 4, which is 1 * 2 * 3 * 4 = 24. Hence, the answer is 24.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first and only line of each test case contains one single integer ‘N’ representing the given integer.
Output Format:
For each test case, print the value of factorial of ‘N’.
Print the output of each test case in a separate line.
1 <= T <= 10
1 <= N <= 100
Time limit: 1 sec
2
4
3
24
6
For the first test case,
The Factorial of 4 is the product of all numbers from 1 to 4, which is 1 * 2 * 3 * 4 = 24. Hence, the answer is 24.
For the second test case,
The Factorial of 3 is the product of all numbers from 1 to 3, which is 1 * 2 * 3 = 6. Hence, the answer is 6.
2
8
11
40320
39916800
Can you find any method to use a linked list in the problem?
In this approach, we will calculate the factorial of the given number and store it in the form of a Linked List of digits named digits. To do this, we will define a function multiply(digits,x) that will multiply the number corresponding to the digits with x and update the digits with the product’s value. Then we will initialize the linked list digits with 1 and multiply it with all numbers from 2 to N using the multiply function. At last, we will print the list in reverse order.
Algorithm:
O(N), where N is the given number.
We are calling multiply function N times, and in each iteration, we are iterating over the whole digits list, which will take constant time. Hence, the overall time complexity is O(N).
O(1).
In this approach, we used a linked list to store the digits of the factorial value. In the worst case, the digits linked list contains at most 200 elements. Hence, the overall space complexity is O(1).