what is wrong in this code??
#include<stdio.h>
int main()
{
long int t,n,fact=1,i;
scanf("%d",&t);
for(n=0;n<=t;n++)
{
scanf("%d",&n);
for(i=n;i>=1;i++)
fact= i*fact;
printf("%d\n",fact);
}
return 0;
}
what is wrong in this code??
#include<stdio.h>
int main()
{
long int t,n,fact=1,i;
scanf("%d",&t);
for(n=0;n<=t;n++)
{
scanf("%d",&n);
for(i=n;i>=1;i++)
fact= i*fact;
printf("%d\n",fact);
}
return 0;
}
@ishan412, here are some of your mistakes :
your code is running into infinite loop in the second for loop for(i=n;i>=1;i++ ) in this case i will always be greater than 1 so the loop never stops running. You need to decrement the value of i.
you have to declare fact = 1 for each test case otherwise it will compute the result using the previous value.
after the above corrections your code will give wrong answer for n>20 because the value of factorial will be larger than 1018 so no data type in c/c++ can store such large number. Therefore to avoid this problem you need to use character array or string.
Read here - Tutorial for computing factorials of very large numbers.
Hope this helps.
#include<stdio.h>
int main()
{
int t,i,n[100],f=1,j=1;
for(i=0;i<t;i++)
{
scanf("%d",&n[i]);
while(j<=n[i])
{
f=f*j;
j++;
}
printf("%d\n",f);
}
return 0;
}
can someone tell me whats wrong with the code?
Read the above editorial and try to understand.
Okay Thanks