# Solution Guide

You can see the practice problems list and select a problem from there. Then, you need to select the language from tabs and write your code in the editor. You can compile and test your code on sample testcases by clicking on 'Compile & Test'. You can submit your code for judge by clicking on 'Submit'. You can refer to Judge Environment for further details.

We will describe solutions for Small Factorials in different languages for successful submission.

Small Factorials
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

Solutions in different languages for Small Factorials are written below.

C

#include <stdio.h>

int main()
{
int kases;
scanf("%d", &kases);
int kase;
for(kase = 1; kase <= kases; kase++) {
int  N;
scanf("%d", &N);
int result[1000];
result[0] = 1;
int length = 1, i, j, temp, carry = 0;
for(i = 2; i <= N; i++) {
for(j = 0; j < length; j++) {
temp = carry + result[j] * i;
carry = temp / 10;
result[j] = temp % 10;
}
while(carry) {
result[j] = carry % 10;
carry /= 10;
j++;
}
length = j;
}
for(int i = length - 1; i >= 0; i--){
printf("%d", result[i]);
}
printf("\n");
}
return 0;
}


C++

#include <iostream>
#include <vector>
using namespace std;

int main()
{
int kases;
cin >> kases;
int kase;
for(kase = 1; kase <= kases; kase++) {
int  N;
cin >> N;
vector result;
result.push_back(1);
int temp, carry = 0;
for(int i = 2; i <= N; i++) {
for(int j = 0; j < result.size(); j++) {
temp = carry + result[j] * i;
carry = temp / 10;
result[j] = temp % 10;
}
while(carry) {
result.push_back(carry % 10);
carry /= 10;
}
}
for(int i = result.size() - 1; i >= 0; i--){
cout << result[i];
}
cout << endl;
}
return 0;
}


Clojure

(defn parse-int [s]
(Integer. (re-find  #"\d+" s )))
(defn fact
([n acc] (if (< n 2) acc (recur (dec n) (* acc n))))
([n] (fact n 1)))
(defmacro for-loop [[sym init check change :as params] & steps]
(loop [~sym ~init value# nil]
(if ~check
(let [new-value# (do ~@steps)]
(recur ~change new-value#))
value#)))
(for-loop [i 0 (< i kases) (inc i)]


C#

using System;
using System.Collections.Generic;
class MyClass {

static void Main(string[] args) {
for (int kase = 1; kase <= kases; kase++) {
List<int> result = new List<int>();
int temp, carry = 0;
for(int i = 2; i <= N; i++) {
for(int j = 0; j < result.Count; j++) {
temp = carry + result[j] * i;
carry = temp / 10;
result[j] = temp % 10;
}
while(carry > 0) {
carry /= 10;
}
}
for(int i = result.Count - 1; i >= 0; i--){
Console.Write(result[i]);
}
Console.Write("\n");

}
}
}


Go

package main

import (
"fmt"
)

func Factorial(n uint64)(result uint64) {
if (n > 0) {
result = n * Factorial(n-1)
return result
}
return 1
}

func main() {
var no_of_test_cases int
var num uint64

fmt.Scanf("%d",&no_of_test_cases)
for i := 0; i < no_of_test_cases; i++ {
fmt.Scanf("%d",&num)
}
for i := 0; i < no_of_test_cases; i++ {
}
}


module Main
where

factorial 1 = 1
factorial n = n * factorial (n - 1)

f xs = concat $map (\x -> show x ++ "\n")$ map factorial $map (\x -> read x :: Integer)$ words xs

main = do
getLine
interact f


Java

  import java.io.BufferedReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

class TestClass {
public static void main(String args[]) throws Exception {
int N = Integer.parseInt(line);
ArrayList<Integer> inputs = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
}
ArrayList<BigInteger> outputs = new ArrayList<BigInteger>();
for(Integer input : inputs){
}
for(BigInteger result: outputs){
System.out.println(result);
}
}

private static BigInteger factorial(Integer input) {
if(input == 1) return BigInteger.ONE;
return factorial(input - 1).multiply(new BigInteger(String.valueOf(input)));
}
}


Java 8

import java.util.stream.LongStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class HelloWorld{

public static void main(String args[]) throws Exception {
int N = Integer.parseInt(line);
ArrayList<Integer> inputs = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
}
ArrayList<BigInteger> outputs = new ArrayList<BigInteger>();
for(Integer input : inputs){
}
for(BigInteger result: outputs){
System.out.println(result);
}
}

private static BigInteger factorial(Integer input) {
if(input == 1) return BigInteger.ONE;
return factorial(input - 1).multiply(new BigInteger(String.valueOf(input)));
}
}


JavaScript (Rhino)

// Equivalent in effect to the Java declaration import java.io.*;
importPackage(java.io);
importPackage(java.lang);
importPackage(java.math);
importPackage(java.util);

var sc = new Scanner(System['in']);
var kases = sc.nextInt();
for(var kase = 1; kase <= kases; kase++) {
var t = sc.nextInt();
var bi=BigInteger.valueOf(1);
for(var i=1; i<=t; i++)
{
bi = bi.multiply(BigInteger.valueOf(i));
}
System.out.println(bi);
}


Javascript (Node.js)

process.stdin.resume();

var i = 0, t = 0;

process.stdin.on('data', function (n) {
if (t == 0) {
t = n;
} else {
if (i++ < t) {
process.stdout.write(Factorial(n).toString());
if (i == t) {
process.exit();
}
} else {
process.exit();
}
}
});

function Factorial (n) {
if (n > 1) {
n *= Factorial(n - 1);
}
return n;
}


Kotlin

import java.math.BigInteger

fun main(args: Array<String>){
while(t-->0){
println(factorial(x))
}
}

fun factorial(x: Int): BigInteger{
var ans=BigInteger.ONE
for(i in 2..x) ans=ans.multiply(BigInteger.valueOf(i.toLong()))
return ans
}


Lisp (sbcl)

(defun factorial (n)
(if (eq n 1) 1
(* (factorial (- n 1)) n)))

(defun main ()
(loop
(setq N (- N 1))
(terpri)
(when (eql N 0) (return))))

(main)


Lua

function factorial(n)
if (n == 0) then
return 1
else
return n * factorial(n - 1)
end
end

for i=0,no_of_test_cases-1,1
do
end

for i=0,no_of_test_cases-1,1
do
io.write('\n')
end


Objective-C

#import <stdio.h>
@interface Factorial
{

}
-(int) f:(int)x;
@end
@implementation Factorial
-(int) f:(int)x {
int N = x;
int result[1000];
result[0] = 1;
int length = 1, i, j, temp, carry = 0;
for(i = 2; i <= N; i++) {
for(j = 0; j < length; j++) {
temp = carry + result[j] * i;
carry = temp / 10;
result[j] = temp % 10;
}
while(carry) {
result[j] = carry % 10;
carry /= 10;
j++;
}
length = j;
}
for(int i = length - 1; i >= 0; i--){
printf("%d", result[i]);
}
printf("\n");
return 0;

}
@end
int main()
{
int kases;
scanf("%d", &kases);
int kase;
for(kase = 1; kase <= kases; kase++) {
int  N;
scanf("%d", &N);
[Factorial f:N];
}
return 0;
}


Perl

use bignum;
my $t = ; my$f;
my @arr;
my $prod=1; for my$i (1..100)
{
$prod=$prod*$i;$arr[$i]=$prod;
}

$arr[0]=0; while($t--)
{
$f=; print$arr[$f],"\n"; }  PHP <?php$save_factorial=array();
function factorial($n) { global$save_factorial;
if($n==1||$n==0)
return 1;
if(isset($save_factorial[$n]))
return $save_factorial[$n];
$save_factorial[$n]=bcmul($n,factorial($n-1));
return $save_factorial[$n];
}

$t=trim(fgets(STDIN)); for($k=1;$k<=$t;$k++) {$i=trim(fgets(STDIN));
echo factorial(\$i)."\n";
}
?>


Python

kases = int(raw_input())
for kase in range(kases):
N = int(raw_input())
result = 1
for i in range(1, N + 1):
result = result * i
print result


R

factorial <- function(n) {
f = 1
for (i in 2:n) f <- f * i
f
}

factorial(6)


Ruby

i = gets.chomp
a = Array.new
i.to_i.times do
a << gets.chomp.to_i
end

a.each do |element|
fact = 1
for i in 1..element
fact = fact * i
end
puts fact
end


Rust

fn factorial_recursive (n: u64) -> u64 {
match n {
0 => 1,
_ => n * factorial_recursive(n-1)
}
}

fn factorial_iterative(n: u64) -> u64 {
(1..n+1).fold(1, |p, n| p*n)
}

fn main () {
for i in 1..10 {
println!("{}", factorial_recursive(i))
}
for i in 1..10 {
println!("{}", factorial_iterative(i))
}
}


Swift

func factorial(number: Int) -> (Int) {
if (number <= 1) {
return 1
}

return number * factorial(number - 1)
}

for item in 1...test_cases {
`