@rased_299 The problem is in this line:

```
factorial = factorial * i;
```

What happens if N is large enough to cause `factorial`

to overflow.

@rased_299 The problem is in this line:

```
factorial = factorial * i;
```

What happens if N is large enough to cause `factorial`

to overflow.

```
#include <stdio.h>
#include<math.h>
int main() {
int N,i,b=0;
long long int fact=1,a=0;
scanf("%d",&N);
for(i=1;i<=N;i++){
fact=fact*i;
}
for(i=0;i<4;i++)
{
a=fact%10;
b=b+a*pow(10,i);
fact=fact/10;
}
printf("%lld",b);
return 0;
}
```

```
#include <stdio.h>
int main()
{
int a,b,c=1;
scanf("%d",&a);
for(b=1;b<=a;b++){
c=c*b;
}
printf("%d",c);
return 0;
}
```

problem???

You have to print only last four digit.

how can i do that ???

`c = c % 10000;`

this will give you last four digit.

```
#include <stdio.h>
int main()
{
int a,b,c=1,d;
scanf("%d",&a);
for(b=1;b<=a;b++){
c=c*b;
}
d=c%10000;
printf("%d",d);
return 0;
}
```

wrong for test 3. Why??

Well you see that factorial of let’s say 100! which is really big the variable `c`

can’t contain this big integer so it will give wrong values.To avoid this you can write `c = (c * b) % 10000`

in the loop, so it will always be four digit integer.

@SonicTitan03

YOu have to feel the factorial closely to solve this problem.

Try to calculate higher values of factorial and check out what their trailing digits become.

1 Like

```
#include <stdio.h>
int main()
{
int n, a;
long long int fact = 1;
scanf("%d", &n);
for(a = n; a >= 2; a--) {
fact = fact * a;
}
printf("%lld", fact%10000);
return 0;
}
```

WHAT IS THE PROBLEM IN THIS CODE.

IT IS SHOWING WRONG ANSWER IN TEST CASE 4

See what @touhidur said above your post. Always go through existing posts to see if you can get any clue. Often you will find that whatever question you have has already been asked by someone else and answered.

if the num>19 what i will print?because

leading is 0 not allowed

//um passing time with the chili sauce

#include<bits/stdc++.h>

using namespace std;

#define io_fast() ios :: sync_with_stdio(false); cin.tie(0);

#define go return 0

#define ll long long int

#define ull unsigned long long int

#define ld long double

#define vi vector

#define vl vector

#define vc vector

#define vs vector

#define vp vector< pair<ll,ll> >

#define sl set

#define sc set

#define pi 3.14159265358979

int fact(int n){

if(n > 1)

return n * fact(n - 1);

else

return 1;

}

int main()

{

io_fast();

ll n,m;

cin>>n;

if(n>19) cout<<0000<<"\n";

else if(n==15) cout<<8000<<"\n";

else {

m=fact(n);

m=m%10000;

cout<<m<<"\n";

}

go;

}

whats the pb?

```
if(n>19) cout<<0000<<"\n";
^^^^
```

I think this will only print 0 there, not 0000.

data type mistake…i could find it out…

Datatype mistake? Do you think you can store 999! with any datatype in C++? Your code will not work. Try to find a more efficient way to solve this problem.

Note: Here is 999! which contains around 2500 (~10^{2500}) digits where `long double`

can only hold a maximum 1.7×10^{308}.

```
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
int main()
{
int n;
cin>>n;
ll k = factorial(n);
stringstream ss;
ss<<k;
string s;
ss>>s;
if(s.size() <= 4) cout<<s<<endl;
else cout<<s.substr(s.length() - 4)<<endl;
return 0;
}
```

What’s wrong here? I am getting “Wrong Answer” on 4th test case.

```
import java.util.Scanner;
public class big_factorials{
public static void main(String[]args){
Scanner input=new Scanner(System.in);
int N=input.nextInt();
if(N>0 && N<1000){
int c=1;
int f=1;
while(c<=N){
f=f*c;
c++;
}
int fac=(f%10000);
System.out.println(fac);
}
}
}
```

where is the error?!

#include <stdio.h>

int main() {

int n,i,fact=1;

scanf("%d",&n);

for(i=1;i<=n;i++){

fact = fact * i;

}

```
printf("%d",fact % 10000);
return 0;
```

}

What is the problem in this code?

It is showing wrong answer in test case #3