Search This Blog

Monday 5 January 2015

SMALL FACTORIALS

PROBLEM:- SMALL FACTORIALS

You are asked to Calculate the factorials of small positive numbers in the range of 100.

INPUT- First line contains an integer T which represents the number of test cases, followed by T lines each containing a single positive Integer.

 OUTPUT- For each integer, print the factorial of that number.

eg:-
Input:-
2 (test cases)
3
4

Output:-
6
24



SOLUTION:-


#include<stdio.h>
int index;
int main()
{
int t,num,temp,x;
scanf("%d",&t);
while(t--)
{
scanf("%d",&num);
int arr[200]={0};
arr[0]=1;
int i=1;
int current=0;
while(i<=num)
{
index=temp=0;
while(arr[index]>0 || index<current)
{
x=(arr[index]*i)+temp;
arr[index]=x%10;
temp=x/10;
index++;
}
while(temp>0)
{
arr[index]=temp%10;
temp/=10;
index++;
}
i++;
current=index-1;
}
index--;
while(index>=0)
{
printf("%d",arr[index]);
index--;
}
printf("\n");
}
return 0;
}