Find last digit in factorial
Given a number n, we need to find the last digit in factorial n.
Input : n = 4
Output : 4
4! = 4 * 3 * 2 * 1. = 24. Last digit of 24 is 4.
Input : n = 5
Output : 5
5! = 5*4 * 3 * 2 * 1. = 120. Last digit of 120 is 0.
A Naive Solution is to first compute fact = n!, then return the last digit of the result by doing fact % 10. This solution is inefficient and causes integer overflow for even slightly large value of n.
C++
#include <iostream>
using namespace std;
int lastDigitFactorial(unsigned int n)
{
if (n == 0) return 1;
int fact = 1;
for ( int i = 1; i <= n; i++) {
fact *= i;
}
return fact % 10;
}
int main() {
cout<<lastDigitFactorial(6);
return 0;
}
|
Java
public class Main {
public static int lastDigitFactorial( int n) {
if (n == 0 ) return 1 ;
int fact = 1 ;
for ( int i = 1 ; i <= n; i++) {
fact *= i;
}
return fact % 10 ;
}
public static void main(String[] args) {
System.out.println(lastDigitFactorial( 6 ));
}
}
|
Python3
def last_digit_factorial(n):
if n = = 0 :
return 1
fact = 1
for i in range ( 1 , n + 1 ):
fact * = i
return fact % 10
print (last_digit_factorial( 6 ))
|
C#
using System;
public class GFG {
public static int LastDigitFactorial( int n) {
if (n == 0) return 1;
int fact = 1;
for ( int i = 1; i <= n; i++) {
fact *= i;
fact = fact % 10;
}
return fact % 10;
}
public static void Main( string [] args) {
Console.WriteLine(LastDigitFactorial(6));
}
}
|
Javascript
function lastDigitFactorial(n) {
if (n === 0) return 1;
let fact = 1;
for (let i = 1; i <= n; i++) {
fact *= i;
}
return fact % 10;
}
console.log(lastDigitFactorial(6));
|
Output
0
Time complexity:
Auxiliary space:
An Efficient Solution is based on the observation that all factorials after 5 have 0 as last digit.
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
…………..
C++
#include <iostream>
using namespace std;
int lastDigitFactorial(unsigned int n)
{
if (n == 0) return 1;
else if (n <= 2) return n;
else if (n == 3) return 6;
else if (n == 4) return 4;
else return 0;
}
int main() {
cout<<lastDigitFactorial(6);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int lastDigitFactorial( int n)
{
if (n == 0 ) return 1 ;
else if (n <= 2 ) return n;
else if (n == 3 ) return 6 ;
else if (n == 4 ) return 4 ;
else return 0 ;
}
public static void main(String[] args)
{
System.out.println(lastDigitFactorial( 6 ));
}
}
|
Python3
def lastDigitFactorial(n):
if (n = = 0 ): return 1
elif (n < = 2 ): return n
elif (n = = 3 ): return 6
elif (n = = 4 ): return 4
else : return 0
print (lastDigitFactorial( 6 ))
|
C#
using System;
class GFG{
static int lastDigitFactorial( int n)
{
if (n == 0) return 1;
else if (n <= 2) return n;
else if (n == 3) return 6;
else if (n == 4) return 4;
else return 0;
}
public static void Main( string [] args)
{
Console.Write(lastDigitFactorial(6));
}
}
|
Javascript
<script>
function lastDigitFactorial(n)
{
if (n == 0) return 1;
else if (n <= 2) return n;
else if (n == 3) return 6;
else if (n == 4) return 4;
else return 0;
}
document.write(lastDigitFactorial(6));
</script>
|
Time Complexity: O(1), since no loop is used hence the algorithm takes constant time to execute
Auxiliary Space: O(1), since no extra array is used hence constant space is used
Approach 2: Using Loop:
The current approach works well for small values of n but is not efficient for large values of n. One way to optimize this approach is to compute the last digit of the factorial using modular arithmetic. The last digit of the factorial is the same as the last digit of the product of all the numbers from 1 to n.
Here’s the optimized approach:
C++
#include <iostream>
using namespace std;
int lastDigitFactorial(unsigned int n) {
if (n == 0 || n == 1) {
return 1;
}
int lastDigit = 1;
for ( int i = 2; i <= n; i++) {
lastDigit = (lastDigit * i) % 10;
}
return lastDigit;
}
int main() {
cout << lastDigitFactorial(6);
return 0;
}
|
Java
public class Main {
static int lastDigitFactorial( int n) {
if (n == 0 || n == 1 ) {
return 1 ;
}
int lastDigit = 1 ;
for ( int i = 2 ; i <= n; i++) {
lastDigit = (lastDigit * i) % 10 ;
}
return lastDigit;
}
public static void main(String[] args) {
System.out.println(lastDigitFactorial( 6 ));
}
}
|
Python3
def lastDigitFactorial(n):
if n = = 0 or n = = 1 :
return 1
lastDigit = 1
for i in range ( 2 , n + 1 ):
lastDigit = (lastDigit * i) % 10
return lastDigit
print (lastDigitFactorial( 6 ))
|
C#
using System;
class Program {
static int LastDigitFactorial( int n) {
if (n == 0 || n == 1) {
return 1;
}
int lastDigit = 1;
for ( int i = 2; i <= n; i++) {
lastDigit = (lastDigit * i) % 10;
}
return lastDigit;
}
static void Main( string [] args) {
Console.WriteLine(LastDigitFactorial(6));
}
}
|
Javascript
function lastDigitFactorial(n) {
if (n == 0 || n == 1) {
return 1;
}
let lastDigit = 1;
for (let i = 2; i <= n; i++)
{
lastDigit = (lastDigit * i) % 10;
}
return lastDigit;
}
console.log(lastDigitFactorial(6));
|
Output:
0
Time Complexity: O(N)
Auxiliary Space: O(1), since no extra array is used hence constant space is used
Now try below problems
1) Last non-zero digit in factorial
2) Count zeroes in factorial
3) First digit in Factorial
Last Updated :
31 Aug, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...