Big Factorials - Easy Problems - Solution to Toph.co Online Judge Problem

Big Factorials

Limits 1s, 512 MB

Given an integer N, print the trailing 4 digits of N! N! (N factorial).
N!=N×(N1)×(N2)×...×1 N! = N \times (N-1) \times (N-2) \times ... \times 1
Here are some examples of N! N! and their last 4 digits.
NN! N! Last 4 Digits
366
750405040
11399168006800
1513076743680008000

Input

The input will contain an integer N (0 < N < 1000).

Output

Print the last 4 digits of N! N! .
Do not print any leading zeroes.

#include<iostream>

using namespace std;

void fact(int n)
{
  int a[2000],temp,c,i;
  a[0]=1;
  c=0;
  for(;n>=2;n--)
  {
      temp=0;
      for(i=0;i<=c;i++)
      {
      temp += (a[i]*n);
      a[i] = temp%10;
      temp = temp/10;
      }
      while(temp>0)
      {
          a[++c]=temp%10;
          temp/=10;
      }
  }

  if(c>=3)
  {
      for(i=3;i>=0;i--)
    cout<<a[i];
  }
  else
    for(i=c;i>=0;i--)
    cout<<a[i];

}


int main()
{
    int n;
    cin>>n;
    fact(n);
}