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 (~102500) digits where long double
can only hold a maximum 1.7×10308.
#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.
#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
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press “Run” button to compile and execute it.
*******************************************************************************/
#include
using namespace std;
int main()
{
int i,j,sum=1,k=0;
cin>>i;
for(j=i;j>=1;j–){
sum*=j;
}
if(sum>1000){
cout<<sum%10000;
}
else{
cout<<sum;
}
return 0;
}
help
Did it first try without help… this was fun.
I actually had the same problem. I still didn’t figure it out where the problem actually is but while debugging I saw that the last four digits of factorial of any number greater than 20 is 0000 but my code was not giving this answer thats why i wrote this code and it worked out. But i still didn’t figure out why the factorial of numbers like 23,24 coming wrong in the past code.
This is my final code:
#include
using namespace std;
int main() {
unsigned long long int y;
unsigned long long int p =1;
cin>>y;
if(y>20){cout<<“0”;}
else{
for(int i = 1;i<y+1;i++){ p = p*i;}
int u=p%10000;
cout<< u;
}
return 0;
}
Hope this helps .
#include<stdio.h>
int main(){
unsigned long int e,p=1,n,a;
scanf(“%ld”, &n);
if(n>=0){
for(e=1;e<=n;e++)
p=p*e;
}
if(p<1)
printf(“%d”, p);
else
a=p%10000;
printf(“%ld”, a);
return 0;
}
why this is not working for case 4
Sir, I have no idea how to print last four digit of factorial in python… I tried from my own but failed a lot of time, give me a hint… here is my code what I wrote
#factorial
import math
n1 = int(input())
print(math.factorial(n1))
@abir123 The following prints the last digit:
N = 1234567
print(N % 10)
That’s a hint I can give you.
#thanks and lot
i = 1
for i in range(1, 10):
i = i + 1
print(“Thanks a lot”)
What is the problem of my code???
#include <stdio.h>
unsigned long long int factorial (unsigned long long int n) {
if (n == 0)
return 1;
else
return n * factorial (n - 1);
}
int main()
{
unsigned long long int n;
scanf ("%llu", &n);
int k=factorial(n)%10000;
printf ("%llu", k);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
cin>>n;
cout<<(n>19?“0000”:n==8?“0320”:n==13?“0800”:to_string(((long long int)tgamma(n+1))%10000))<<endl;
}
why does it output 8799 for 10 on toph whereas it outputs 8800 everywhere else?
#include <stdio.h>
int main()
{
int i, j, a[4];
int n, x;
unsigned long long int f=1;
scanf("%d", &n);
for(i=n;i>=1;i--){
f=f*i;
}
if(f>9999){
for(j=0;j<4;j++){
x=f%10;
f=f/10;
a[j]=x;
}
printf("%d%d%d%d", a[3], a[2], a[1], a[0]);
}
else
printf("%llu", f);
}
What is the problem of this code
#include <stdio.h>
int main()
{
int i, j, a[4];
int n, x;
unsigned long long int f=1;
scanf("%d", &n);
for(i=n;i>=1;i--){
f=f*i;
}
if(f>9999){
for(j=0;j<4;j++){
x=f%10;
f=f/10;
a[j]=x;
}
if(a[3]!=0){
printf("%d%d%d%d", a[3], a[2], a[1], a[0]);
}
if(a[3]==0) {printf("");}
}
else
printf("%llu", f);
}
Why wrong answer in test 4
Using long long integers is not the solution.
You need to assign only 4 digits in fact for the next multiplication with i. So, you just have to add fact%=10000 after fact=fact*i.
My code is stuck at testcase 4. I’d like to know what is the test input at testcase 4.
#include<stdio.h>
#include<bits/stdc++.h>
#include<iostream>
#define f(i,s,n) for(int i=s;i<n;i++)
#define fr(i,s,n) for(int i=s;i>=n;i--)
using ll= long long;
using ch=char;
using namespace std;
int main()
{
long long a;
scanf("%lld",&a);
ll res=1,c=0,d;
for(long long i=a;i>=2;i--)
{
res=res*(i%10000);
c++;
if(c==4){res=res%10000;c=0;}
}
if(res%10000==0)printf("%04lld\n",res%10000);
else printf("%lld\n",res%10000);
}
What’s wrong here?