# Third last digit in 5^N for given N

Given a positive integer N. The task is to find the value of 3^{rd} digit from last (right-most) of 5^{N}.

**Examples:**

Input :N = 6Output :6 Explanation : Value of 5^{6}= 15625.Input :N = 3Output :1 Explanation : Value of 5^{3}= 125.

**Approach :** Before moving to actual approach, some facts regarding number theory are listed below as:

- 5
^{3}is the smallest 3 digit number which is power of 5. - As 125 * 5 = 625, this conclude that multiple of number (ending with 125) with 5 always construct 625 as last three digit of result.
- Again as, 625 * 5 = 3125, this conclude that multiple of number (ending with 625) with 5 always construct 125 as last three digit of result.

Hence, the final general solution is :

case 1: if

n < 3, answer = 0.

case 2: ifn >= 3 and is even, answer = 6.

case 3: ifn >= 3 and is odd, answer = 1.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the element ` `int` `findThirdDigit(` `int` `n) ` `{ ` ` ` `// if n < 3 ` ` ` `if` `(n < 3) ` ` ` `return` `0; ` ` ` ` ` `// If n is even return 6 ` ` ` `// If n is odd return 1 ` ` ` `return` `n & 1 ? 1 : 6; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 7; ` ` ` ` ` `cout << findThirdDigit(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the ` `// above approach ` `class` `GFG ` `{ ` ` ` `// Function to find the element ` `static` `int` `findThirdDigit(` `int` `n) ` `{ ` ` ` `// if n < 3 ` ` ` `if` `(n < ` `3` `) ` ` ` `return` `0` `; ` ` ` ` ` `// If n is even return 6 ` ` ` `// If n is odd return 1 ` ` ` `return` `(n & ` `1` `) > ` `0` `? ` `1` `: ` `6` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `7` `; ` ` ` ` ` `System.out.println(findThirdDigit(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the ` `# above approach ` ` ` `# Function to find the element ` `def` `findThirdDigit(n): ` ` ` ` ` `# if n < 3 ` ` ` `if` `n < ` `3` `: ` ` ` `return` `0` ` ` ` ` `# If n is even return 6 ` ` ` `# If n is odd return 1 ` ` ` `return` `1` `if` `n ` `and` `1` `else` `6` ` ` `# Driver code ` `n ` `=` `7` `print` `(findThirdDigit(n)) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the element ` `static` `int` `findThirdDigit(` `int` `n) ` `{ ` ` ` `// if n < 3 ` ` ` `if` `(n < 3) ` ` ` `return` `0; ` ` ` ` ` `// If n is even return 6 ` ` ` `// If n is odd return 1 ` ` ` `return` `(n & 1)>0 ? 1 : 6; ` `} ` ` ` `// Driver code ` `static` `void` `Main() ` `{ ` ` ` `int` `n = 7; ` ` ` ` ` `Console.WriteLine(findThirdDigit(n)); ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

1

## Recommended Posts:

- Count of Numbers in Range where first digit is equal to last digit of the number
- Largest number less than N with digit sum greater than the digit sum of N
- Count 'd' digit positive integers with 0 as a digit
- Count n digit numbers not having a particular digit
- Check if frequency of each digit is less than the digit
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Last non-zero digit of a factorial
- Program for replacing one digit with other
- Recursive sum of digit in n^x, where n and x are very large
- Sum of digit of a number using recursion
- First occurrence of a digit in a given fraction
- Least Greater number with same digit sum
- Special two digit number
- First digit in factorial of a number
- Numbers having difference with digit sum more than s

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.