My issue
My code
#include<iostream>
using namespace std;
int main()
{
int t,n;
cin>>t;
int k=0;
while(k<t){
cin>>n;
int product=1;
for(int i=1;i<=n;i++)
{
product=product*i;
}
cout<<product<<endl;
k++;
}
}
Problem Link: FCTRL2 Problem - CodeChef
your code is mostly correct except for test cases and you wrote cin>>n inside the loop, write while(t–) and it will be correct,
int main(){
int t,n;
cin>>t>>n;
while(t–){
int prod=1;
for(int i=1;i<=n;i++){
prod = prod*i //
}
cout <<prod <<endl;}
}
return 0;
}
thank u so much but pls tell me fault in my while loop
@kjha1893 look the while loop u write just runs for the number of test cases
so , if u have 3 test cases it should run 3 times. why use extra variables and complicate things.
just write [ while (t–)] : this condition will get true if t>0 and then it will proceed further and reduce the value of t by 1 .
if us still dont get it you can just simply write while(t>0) and then just write t-- in the loop
this should do the same .
hope this helps !!
The issue is overflow. Your code will work for small n, but for large n like n >= 30 (or something), data types like int and long long will overflow. So in order to avoid that you can use a boost multi-precision library, or use an alternative (which could be difficult to implement) to use an array having each digit in each index of the array and multiply them using a for loop. This problem is same as SPOJ’s small factorials problem.
but what is boost multi-precision