#include<bits/stdc++.h>
using namespace std;
int d[] = {1,1,2,6,4,2,2,4,2,8};
int last_Non_Zero_Digit(int n)
{
if(n < 10)
return d[n];
if(((n/10)%10)%2 == 0)
return (6*last_Non_Zero_Digit(n/5)*d[n%10])%10;
else
return (4*last_Non_Zero_Digit(n/5)*d[n%10])%10;
}
int main()
{
int n;
while(cin >> n)
{
int p = last_Non_Zero_Digit(n);
cout << "Last Non Zero Digit is of " << n << " Factorial : " << p << endl;
}
return 0;
}
using namespace std;
int d[] = {1,1,2,6,4,2,2,4,2,8};
int last_Non_Zero_Digit(int n)
{
if(n < 10)
return d[n];
if(((n/10)%10)%2 == 0)
return (6*last_Non_Zero_Digit(n/5)*d[n%10])%10;
else
return (4*last_Non_Zero_Digit(n/5)*d[n%10])%10;
}
int main()
{
int n;
while(cin >> n)
{
int p = last_Non_Zero_Digit(n);
cout << "Last Non Zero Digit is of " << n << " Factorial : " << p << endl;
}
return 0;
}
Comments
Post a Comment