Skip to content
Related Articles

Related Articles

Find last digit in factorial
  • Difficulty Level : Medium
  • Last Updated : 26 Feb, 2021

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.

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++




// C++ program to find last digit in
// factorial n.
#include <iostream>
using namespace std;
 
int lastDigitFactorial(unsigned int n)
{
   // Explicitly handle all numbers
   // less than or equal to 4
   if (n == 0) return 1;
   else if (n <= 2) return n;
   else if (n == 3) return 6;
   else if (n == 4) return 4;
   
   // For all numbers greater than 4
   // the last digit is 0
   else return 0;
}
 
int main() {
 
    cout<<lastDigitFactorial(6);
    return 0;
}

Java




// Java program to find last 
// digit in factorial n.
import java.io.*;
import java.util.*;
 
class GFG {
     
static int lastDigitFactorial(int n)
{
     
    // Explicitly handle all numbers
    // less than or equal to 4
    if (n == 0) return 1;
    else if (n <= 2) return n;
    else if (n == 3) return 6;
    else if (n == 4) return 4;
     
    // For all numbers greater than
    // 4 the last digit is 0
    else return 0;
}
 
// Driver code
public static void main(String[] args)
{
    System.out.println(lastDigitFactorial(6));
}
}
 
// This code is contributed by coder001

Python3




# Python3 program to find last digit in
# factorial n.
 
def lastDigitFactorial(n):
     
    # Explicitly handle all numbers
    # less than or equal to 4
    if (n == 0): return 1
    elif (n <= 2): return n
    elif (n == 3): return 6
    elif (n == 4): return 4
     
    # For all numbers greater than 4
    # the last digit is 0
    else: return 0
 
print(lastDigitFactorial(6))
 
# This code is contributed by divyeshrabadiya07

C#




// C# program to find last
// digit in factorial n.
using System;
class GFG{
     
static int lastDigitFactorial(int n)
{
     
    // Explicitly handle all numbers
    // less than or equal to 4
    if (n == 0) return 1;
    else if (n <= 2) return n;
    else if (n == 3) return 6;
    else if (n == 4) return 4;
     
    // For all numbers greater than
    // 4 the last digit is 0
    else return 0;
}
 
// Driver code
public static void Main(string[] args)
{
    Console.Write(lastDigitFactorial(6));
}
}
 
// This code is contributed by rutvik_56

Javascript




<script>
 
// JavaScript program to find last digit in
// factorial n.
 
function lastDigitFactorial(n)
{
    // Explicitly handle all numbers
    // less than or equal to 4
    if (n == 0) return 1;
    else if (n <= 2) return n;
    else if (n == 3) return 6;
    else if (n == 4) return 4;
     
    // For all numbers greater than 4
    // the last digit is 0
    else return 0;
}
 
    // Driver code 
    document.write(lastDigitFactorial(6));
     
// This code is contributed by Surbhi Tyagi
 
</script>
Output: 
0

 

Time Complexity : O(1)
Auxiliary Space : O(1)

Now try below problems

1) Last non-zero digit in factorial
2) Count zeroes in factorial
3) First digit in Factorial

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :