Facto sum problem daily byte code

questions:https://hack.codingblocks.com/dcb/p/56
answers:https://ide.codingblocks.com/#/s/23682
2/6 test cases

2 mistakes:

  1. fact must be initialised inside the ā€˜iā€™ loop.
  2. Basically you can not compute factorial of numbers >10 and store them in integer format. Thus you should take mod 107 while computing factorial itself.

Here is the corrected version.

#include
using namespace std;

int main() {
int n,fact=1,sum=0,arr[10];
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=0;i<n;i++)
{
fact=1;
for(int j=1;j<=arr[i];j++)
{ fact=(fact*j)%107;

}
sum=(sum+fact)%107;

}
cout<<(sum)%107;
return 0;
}

1 Like