My issue
please tell me which test case is failed
include
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t>0)
{
int n;
cin>>n;
if(n==0){
cout<<1<<endl;
}
else{
int fact=1;
for(int i=n;i>0;i–){
fact=fact*i;
}
cout<<fact<<endl;
}
t--;
}
return 0;
}
My code
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t>0)
{
int n;
cin>>n;
if(n==0){
cout<<1<<endl;
}
else{
int fact=1;
for(int i=n;i>0;i--){
fact=fact*i;
}
cout<<fact<<endl;
}
t--;
}
return 0;
}
Problem Link: FCTRL2 Problem - CodeChef
@shashwat420
U have to think of another logic cozz the factorial of n which is till 100 can’t be stored in any datatype.
U can do this question like this.
#include<bits/stdc++.h>
using namespace std;
void factorial(int n) {
vector<int>ans;
ans.push_back(1);
int carry=0;
int x,digit;
for(int i=2;i<=n;i++){
for(int j=0;j<ans.size();j++){
x=ans[j]*i+carry;
ans[j]=x%10;
carry=x/10;
}
while(carry){
ans.push_back(carry%10);
carry=carry/10;
}
}
reverse(ans.begin(),ans.end());
for(auto i:ans){
cout<<i;
}cout<<"\n";
}
int main(){
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifdef EPSILON
freeopen("inputf.in","r",stdin);
freeopen("outputf.in","w",stdout);
#endif
int t;
cin>>t;
while(t--){
int n;
cin>>n;
factorial(n);
}
return 0;
}
what is the problem with my code
Your code will not work for high values of n .