Open In App

Third last digit in 5^N for given N

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

Input : N = 6
Output : 6
Explanation : Value of 56 = 15625.

Input : N = 3
Output : 1
Explanation : Value of 53 = 125. 

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



Hence, the final general solution is :  

Case 1: if n < 3, answer = 0. 
Case 2: if n >= 3 and is even, answer = 6. 
Case 3: if n >= 3 and is odd, answer = 1.



Below is the implementation of the above approach: 




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




// Java implementation of the
// above approach
import java.io.*;
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




# 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




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




<?php
// PHP implementation of the above approach
 
// Function to find the element
function findThirdDigit($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
$n = 7;
echo findThirdDigit($n);
 
// This code contributed by
// PrinciRaj1992
?>




<script>
 
 // Javascript implementation of the above approach
 
// Function to find the element
function findThirdDigit(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
var n = 7;
document.write( findThirdDigit(n));
 
</script>

Output: 
1

 

Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.


Article Tags :