wrong answer FCTRL2..BUT IT WORKS ON MY COMPILER.COULD YOU PLEASE TELL ME WHERE THE ERROR IS?

,

hi here is the code…
#include<stdio.h>
#include<stdlib.h>
int fact(int n);

int main(void)
{
 int t,*numptr,i;
 scanf("%d",&t);
 if(t<1||t>100)
  return 0;
 numptr=(int *)malloc(sizeof(t)*t);
 for(i=0;i<t;i++)
  {
   scanf("%d",(numptr+i));
   int num=*(numptr+i);
   if(num<1||num>100)
    return 0;
   printf("%d\n",fact(num));
  }
return 0;
}
int fact(int n)
{
 if(n==1)
  return 1;
  return n*fact(n-1);
}

This occurs because of integer overflow. Try finding factorial of say 50 with your program & cross check it against the Windows / or any other calculator. Check out this link for range of values data types in C can handle : http://www.tutorialspoint.com/ansi_c/c_basic_datatypes.htm

To solve this problem you’ll need to use GNU MP Bignum Library, check this link : http://gmplib.org/ or you can design your own container using arrays with functions to perform operations such as addition, multiplication, etc.

Or you can use another programming language with built in BigInteger support such as JAVA, Python, etc. which is what I did, my code in python is here if you’ll like to view it : https://github.com/Ouditchya/SPOJ/blob/master/FCTRL2.py

Hope it helps. :slight_smile:

Regards,

Ouditchya Sinha.

Use array to store the factorials(as fact(100) has 158 digits which is indeed out of range of even long long).Here is my codes: http://www.codechef.com/viewsolution/1860071

//