Skip to content
Related Articles

Related Articles

Find last five digits of a given five digit number raised to power five
  • Difficulty Level : Expert
  • Last Updated : 14 Apr, 2021

Given a five digit number N. The task is to find the last five digits of the given number raised to the power of 5 after modifying it by arranging the digits as: 
 

first digit, third digit, fifth digit, fourth digit, second digit.

Examples: 
 

Input : N = 12345
Output : 71232
Explanation : 
After modification the number becomes 13542. (13542)5 is 
455422043125550171232

Input : N = 10000
Output : 00000

 

Approach : In this problem, just implementation of the actions described in the statement is required. However, there are two catches in this problem.
The first catch is that the fifth power of five-digit number cannot be represented by a 64-bit integer. But we do not actually need the fifth power, we need the fifth power modulo 105. And mod operation can be applied after each multiplication.
The second catch is that you need to output five digits, not the fifth power modulo 105. The difference is when the fifth digit from the end is zero. To output, a number with the leading zero one can either use corresponding formatting (%05d in printf) or extract digits and output them one by one.
Below is the implementation of the above approach :
 

C++




// CPP program to find last five digits
// of a five digit number raised to power five
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the last five digits
// of a five digit number raised to power five
int lastFiveDigits(int n)
{
    n = (n / 10000) * 10000
        + ((n / 100) % 10)
              * 1000
        + (n % 10)
              * 100
        + ((n / 10) % 10)
              * 10
        + (n / 1000) % 10;
 
    int ans = 1;
    for (int i = 0; i < 5; i++) {
        ans *= n;
        ans %= 100000;
    }
 
    printf("%05d", ans);
}
 
// Driver code
int main()
{
    int n = 12345;
 
    lastFiveDigits(n);
 
    return 0;
}

Java




// Java program to find last five digits
// of a five digit number raised to power five
 
class GfG {
 
    // Function to find the last five digits
    // of a five digit number raised to power five
    static void lastFiveDigits(int n)
    {
        n = (n / 10000) * 10000
            + ((n / 100) % 10)
                  * 1000
            + (n % 10)
                  * 100
            + ((n / 10) % 10)
                  * 10
            + (n / 1000) % 10;
 
        int ans = 1;
        for (int i = 0; i < 5; i++) {
            ans *= n;
            ans %= 100000;
        }
 
        System.out.println(ans);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 12345;
 
        lastFiveDigits(n);
    }
}

Python3




# Python3 program to find last five digits
# of a five digit number raised to power five
 
# Function to find the last five digits
# of a five digit number raised to power five
def lastFiveDigits(n):
    n = ((int)(n / 10000) * 10000 +
        ((int)(n / 100) % 10) * 1000 + (n % 10) * 100 +
        ((int)(n / 10) % 10) * 10 + (int)(n / 1000) % 10)
    ans = 1
    for i in range(5):
        ans *= n
        ans %= 100000
    print(ans)
 
# Driver code
if __name__ == '__main__':
    n = 12345
 
    lastFiveDigits(n)
 
# This code contributed by PrinciRaj1992

C#




// C# program to find last five
// digits of a five digit number
// raised to power five
using System;
 
class GFG
{
 
    // Function to find the last
    // five digits of a five digit
    // number raised to power five
    public static void lastFiveDigits(int n)
    {
        n = (n / 10000) * 10000 +
           ((n / 100) % 10) * 1000 +
            (n % 10) * 100 +
           ((n / 10) % 10) * 10 +
            (n / 1000) % 10;
 
        int ans = 1;
        for (int i = 0; i < 5; i++)
        {
            ans *= n;
            ans %= 100000;
        }
 
        Console.WriteLine(ans);
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int n = 12345;
 
        lastFiveDigits(n);
    }
}
 
// This code is contributed
// by Shrikant13

PHP




<?php
// PHP program to find last five digits
// of a five digit number raised to power five
 
// Function to find the last five digits
// of a five digit number raised to power five
function lastFiveDigits($n)
{
    $n = (int)($n / 10000) * 10000 +
        ((int)($n / 100) % 10) * 1000 +
              ($n % 10) * 100 +
        ((int)($n / 10) % 10) * 10 +
         (int)($n / 1000) % 10;
 
    $ans = 1;
    for ($i = 0; $i < 5; $i++)
    {
        $ans *= $n;
        $ans %= 100000;
    }
 
    echo $ans;
}
 
// Driver code
$n = 12345;
 
lastFiveDigits($n);
 
// This code is contributed
// by Akanksha Rai
?>

Javascript




<script>
// JavaScript program to find last five digits
// of a five digit number raised to power five
 
// Function to find the last five digits
// of a five digit number raised to power five
function lastFiveDigits(n)
{
    n = (Math.floor(n / 10000)) * 10000
        + (Math.floor(n / 100) % 10)
            * 1000
        + (n % 10)
            * 100
        + (Math.floor(n / 10) % 10)
            * 10
        + Math.floor(n / 1000) % 10;
 
    let ans = 1;
    for (let i = 0; i < 5; i++) {
        ans *= n;
        ans %= 100000;
    }
 
    document.write(ans);
}
 
// Driver code
 
    let n = 12345;
 
    lastFiveDigits(n);
 
// This code is contributed by Manoj.
 
</script>
Output: 
71232

 

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.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA




My Personal Notes arrow_drop_up
Recommended Articles
Page :