Find last five digits of a given five digit number raised to power five in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a number N. Our task is to find the last five digits of a given five digit number raised to power five. 

Let’s take an example to understand the problem, 

Input: N = 25211

Output:

Solution Approach

To solve the problem, we need to find only the last five digits of the resultant value. So, we will find the last digit of the number after every power increment by finding the 5 digit remainder of the number.  At last return the last 5 digits after power of 5.

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;

int lastFiveDigits(int n) {
   int result = 1;
   for (int i = 0; i < 5; i++) {
      result *= n;
      result %= 100000;
   }
   cout<<"The last five digits of "<<n<<" raised to the power 5 are "<<result;
}

int main() {
   int n = 12345;
   lastFiveDigits(n);
   return 0;
}

Output

The last five digits of 12345 raised to the power 5 are 65625
raja
Published on 25-Jan-2021 04:59:03
Advertisements