# Extra Long Factorials HackerRank Solution in C, C++, Java, Python

The factorial of the integer n, written n!, is defined as:

Calculate and print the factorial of a given integer.

For example, if n=30, we calculate 30*29*28*….*2*1 and get .

Function Description

Complete the extraLongFactorials function in the editor below. It should print the result and return.

extraLongFactorials has the following parameter(s):

n: an integer

Note: Factorials of n>20 can’t be stored even in a 64-bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

We recommend solving this challenge using BigIntegers.

Input Format

Input consists of a single integer n

Constraints

1<=n<=100

Output Format

Print the factorial of n.

Sample Input

`25`

## Extra Long Factorials HackerRank Solution in C

```#include<stdio.h>
int main()
{
int t;
int a[200]; //array will have the capacity to store 200 digits.
int n,i,j,temp,m,x;

//   scanf("%d",&t);
// while(t--)
//{
scanf("%d",&n);
a[0]=1;  //initializes array with only 1 digit, the digit 1.
m=1;    // initializes digit counter

temp = 0; //Initializes carry variable to 0.
for(i=1;i<=n;i++)
{
for(j=0;j<m;j++)
{
x = a[j]*i+temp; //x contains the digit by digit product
a[j]=x%10; //Contains the digit to store in position j
temp = x/10; //Contains the carry value that will be stored on later indexes
}
while(temp>0) //while loop that will store the carry value on array.
{
a[m]=temp%10;
temp = temp/10;
m++; // increments digit counter
}
}
printf("%d",a[i]);
printf("\n");
//}
return 0;
}```

## Extra Long Factorials HackerRank Solution in C++

```#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string multiply(string &num1, string num2) {
string res;
int a, b, c, m, n, l, k, sum, carry;
char d;

m = num1.size() - 1;
n = num2.size() - 1;
carry = 0;
for (int i = m; i >= 0; i--) {
for (int j = n; j >= 0; j--) {
l = res.size() - 1;
a = num1[i] - '0';
b = num2[j] - '0';
k = (m-i) + (n-j);

if (l >= k) c = res[l-k] - '0';
else c = 0;

sum = a * b + c + carry;
carry = sum / 10;
d = char(sum % 10 + '0');

if (l >= k) res[l-k] = d;
else res.insert(0, &d, 1);

if (j == 0 && carry) {
d = char(carry + '0');
res.insert(0, &d, 1);
carry = 0;
}
}
}

return res[0] == '0' ? "0" : res;
}

int main() {

Scanner sc=new Scanner(System.in);
System.out.println(factorial(sc.nextInt()));
}
}```

## Extra Long Factorials HackerRank Solution in Python

```# Enter your code here. Read input from STDIN. Print output to STDOUT
t = input()
num = 1
while t>0:
num=num*t
t=t-1
print num```

## Extra Long Factorials HackerRank Solution in C#

```using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
class Solution
{
static void Main(String[] args)
{
BigInteger Sum = new BigInteger();
Sum = 1;
for (int i = 1; i < Factorial + 1; i++)
{
Sum *= i;
}
Console.WriteLine(Sum);
}
}```

Attempt Extra Long Factorials HackerRank Challenge

Next HackerRank Challenge Solution