Factorials of large numbers
PROBLEM :
Given an integer, the task is to find factorial of the number.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,the number whose factorial is to be found
Output:
Print the factorial of the number in separate line.
Constraints:
1 = T = 100
1 = N = 1000
Example:
Input
3
5
10
2
Output
120
3628800
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 10000
void Factorials_of_large_numbers(int ) ;
int multiply(int arr[],int ,int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
Factorials_of_large_numbers(no) ;
cout<<endl ;
}
return 0;
}
void Factorials_of_large_numbers(int no)
{
int arr[MAX] ;
int size=1,i ;
arr[0]=1 ;
for(i=2;i<=no;i++)
size=multiply(arr,size,i) ;
for(i=size-1;i>=0;i--)
cout<<arr[i] ;
}
int multiply(int arr[],int no,int k)
{
int i,carry,prod ;
carry=0 ;
for(i=0;i<no;i++)
{
prod=arr[i]*k+carry ;
arr[i]=prod%10 ;
carry=prod/10 ;
}
while(carry)
{
arr[no]=carry%10;
carry=carry/10;
no++;
}
return no ;
}
---------------------------------------------------------------------------------
Given an integer, the task is to find factorial of the number.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,the number whose factorial is to be found
Output:
Print the factorial of the number in separate line.
Constraints:
1 = T = 100
1 = N = 1000
Example:
Input
3
5
10
2
Output
120
3628800
2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define MAX 10000
void Factorials_of_large_numbers(int ) ;
int multiply(int arr[],int ,int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
Factorials_of_large_numbers(no) ;
cout<<endl ;
}
return 0;
}
void Factorials_of_large_numbers(int no)
{
int arr[MAX] ;
int size=1,i ;
arr[0]=1 ;
for(i=2;i<=no;i++)
size=multiply(arr,size,i) ;
for(i=size-1;i>=0;i--)
cout<<arr[i] ;
}
int multiply(int arr[],int no,int k)
{
int i,carry,prod ;
carry=0 ;
for(i=0;i<no;i++)
{
prod=arr[i]*k+carry ;
arr[i]=prod%10 ;
carry=prod/10 ;
}
while(carry)
{
arr[no]=carry%10;
carry=carry/10;
no++;
}
return no ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment