Given an integer N, print the trailing 4 digits of N! (N factorial). N!=N×(N−1)×(N−2)×…×1 Here are…
Click here to read the complete problem statement.
If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.
The problem statement is inconsistent Why would it allow 24 instead of 0024? Clearly the problem wanted last 4 digits! And even if it allows 24, why would it only accept 0000 and not 0?
The statement has been updated, but what you described reveals an interesting quirk in our code that compares outputs. We will have to tinker with it a little.
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.
#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.
//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?