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.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.