📜  查找给定数字的阶乘的最后两位数字

📅  最后修改于: 2021-04-29 15:12:54             🧑  作者: Mango

给定整数N ,任务是找到数字的阶乘的最后两位。
例子:

方法:我们可以观察到,对于N> = 10 ,其阶乘的最后两个位置将仅包含0。因此, N!对于任何N> = 10的%100始终为0。因此,如果N <10 ,我们只需计算阶乘并提取最后两位数。
下面是上述方法的实现:

C++
// C++ implementation to
// find last two digits
// factorial of a given number
 
#include 
using namespace std;
 
// Function to print the
// last two digits of N!
void lastTwoDigits(long long N)
{
 
    // For N >= 10, N! % 100
    // will always be 0
    if (N >= 10) {
        cout << "00";
        return;
    }
 
    long long fac = 1;
    // Calculating N! % 100
    for (int i = 1; i <= N; i++)
        fac = (fac * i) % 100;
 
    cout << fac;
}
 
// Driver code
int main()
{
    int N = 7;
    lastTwoDigits(N);
}


Java
// Java implementation to
// find last two digits
// factorial of a given number
import java.util.*;
class GFG{
 
// Function to print the
// last two digits of N!
static void lastTwoDigits(double N)
{
 
    // For N >= 10, N! % 100
    // will always be 0
    if (N >= 10)
    {
        System.out.print("00");
        return;
    }
 
    double fac = 1;
     
    // Calculating N! % 100
    for (int i = 1; i <= N; i++)
        fac = (fac * i) % 100;
 
    System.out.print(fac);
}
 
// Driver code
public static void main(String args[])
{
    int N = 7;
    lastTwoDigits(N);
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation to
# find last two digits
# factorial of a given number
 
# Function to print the
# last two digits of N!
def lastTwoDigits(N):
 
    # For N >= 10, N! % 100
    # will always be 0
    if (N >= 10):
        print("00", end = "")
        return
 
    fac = 1
     
    # Calculating N! % 100
    for i in range(1, N + 1):
        fac = (fac * i) % 100
 
    print(fac)
 
# Driver code
if __name__ == '__main__':
    N = 7
    lastTwoDigits(N)
 
# This code is contributed by Mohit Kumar


C#
// C# implementation to
// find last two digits
// factorial of a given number
using System;
class GFG{
 
// Function to print the
// last two digits of N!
static void lastTwoDigits(double N)
{
 
    // For N >= 10, N! % 100
    // will always be 0
    if (N >= 10)
    {
        Console.Write("00");
        return;
    }
 
    double fac = 1;
     
    // Calculating N! % 100
    for (int i = 1; i <= N; i++)
        fac = (fac * i) % 100;
 
    Console.Write(fac);
}
 
// Driver code
public static void Main()
{
    int N = 7;
    lastTwoDigits(N);
}
}
 
// This code is contributed by Nidhi_biet


Javascript


输出:
40