Given a positive **N**, the task is to find the last two digits of **7 ^{N}**.

**Examples:**

Input:N = 5Output:07Explanation:

The value of 7^{5}= 7 * 7 * 7 * 7 * 7 = 8507

Therefore, the last two digits are 07.Input:N = 12Output:01Explanation:

The value of 7^{12}= 13841287201

Therefore, the last two digits are 01.

**Approach:** A general approach to finding the last **K** digits of **X ^{Y}** is to discuss this article in logarithmic time complexity. In this article, we will discuss the constant time solution.

Below is the observation for the value of 7

^{N}for some values of

**N**:

7

^{1}= 7 last two digit = 07

7^{2}= 49 last two digit = 49

7^{3}= 243 last two digit = 43

7^{4}= 2401 lasr two digit = 01

7^{5}= 16807 last two digit = 07

7^{6}= 117649 last two digit = 49

7^{7}= 823543 last two digit = 43

7^{8}= 5764801 last two digit = 01

Based on the above observations we have the following cases:

- If the last two digit in 7
^{N}= 07 when N = 4K + 3. - If the last two digit in 7
^{N}= 49 when N = 4K + 2. - If the last two digit in 7
^{N}= 43 when N = 4K + 1. - If the last two digit in 7
^{N}= 01 when N = 4K.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the last` `// two digits of 7^N` `string get_last_two_digit(` `int` `N)` `{` ` ` `// Case 4` ` ` `if` `(N % 4 == 0)` ` ` `return` `"01"` `;` ` ` `// Case 3` ` ` `else` `if` `(N % 4 == 1)` ` ` `return` `"07"` `;` ` ` `// Case 2` ` ` `else` `if` `(N % 4 == 2)` ` ` `return` `"49"` `;` ` ` `// Case 1` ` ` `return` `"43"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Number` ` ` `int` `N = 12;` ` ` `// Function Call` ` ` `cout << get_last_two_digit(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG{` `// Function to find the last` `// two digits of 7^N` `public` `static` `String get_last_two_digit(` `int` `N)` `{` ` ` `// Case 4` ` ` `if` `(N % ` `4` `== ` `0` `)` ` ` `return` `"01"` `;` ` ` `// Case 3` ` ` `else` `if` `(N % ` `4` `== ` `1` `)` ` ` `return` `"07"` `;` ` ` `// Case 2` ` ` `else` `if` `(N % ` `4` `== ` `2` `)` ` ` `return` `"49"` `;` ` ` `// Case 1` ` ` `return` `"43"` `;` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `12` `;` ` ` `// Function Call` ` ` `System.out.println(get_last_two_digit(N));` `}` `}` `// This code is contributed by grand_master` |

## Python3

`# Python3 program for the above approach` `# Function to find the last` `# two digits of 7 ^ N` `def` `get_last_two_digit(N):` ` ` `# Case 4` ` ` `if` `(N ` `%` `4` `=` `=` `0` `):` ` ` `return` `"01"` `;` ` ` `# Case 3` ` ` `elif` `(N ` `%` `4` `=` `=` `1` `):` ` ` `return` `"07"` `;` ` ` `# Case 2` ` ` `elif` `(N ` `%` `4` `=` `=` `2` `):` ` ` `return` `"49"` `;` ` ` `# Case 1` ` ` `return` `"43"` `;` ` ` `# Driver Code` `# Given number` `N ` `=` `12` `;` `# Function call` `print` `( get_last_two_digit(N))` `# This code is contributed by grand_master` |

## C#

`// C# program for the above approach` `using` `System;` `namespace` `GFG{` `class` `GFG{` ` ` `// Function to find the last` `// two digits of 7^N` `public` `static` `String get_last_two_digit(` `int` `N)` `{` ` ` `// Case 4` ` ` `if` `(N % 4 == 0)` ` ` `return` `"01"` `;` ` ` `// Case 3` ` ` `else` `if` `(N % 4 == 1)` ` ` `return` `"07"` `;` ` ` `// Case 2` ` ` `else` `if` `(N % 4 == 2)` ` ` `return` `"49"` `;` ` ` `// Case 1` ` ` `return` `"43"` `;` `}` `// Driver code` `public` `static` `void` `Main()` `{` ` ` ` ` `// Given number` ` ` `int` `N = 12;` ` ` `// Function Call` ` ` `Console.Write(get_last_two_digit(N));` `}` `}` `}` `// This code is contributed by grand_master` |

## Javascript

`<script>` `// Javascript program for the above approach` `// Function to find the last` ` ` `// two digits of 7^N` ` ` `function` `get_last_two_digit(N)` ` ` `{` ` ` `// Case 4` ` ` `if` `(N % 4 == 0)` ` ` `return` `"01"` `;` ` ` `// Case 3` ` ` `else` `if` `(N % 4 == 1)` ` ` `return` `"07"` `;` ` ` `// Case 2` ` ` `else` `if` `(N % 4 == 2)` ` ` `return` `"49"` `;` ` ` `// Case 1` ` ` `return` `"43"` `;` ` ` `}` ` ` `// Driver code` ` ` ` ` `var` `N = 12;` ` ` `// Function Call` ` ` `document.write(get_last_two_digit(N));` `// This code contributed by gauravrajput1` `</script>` |

**Output:**

01

**Time Complexity:** *O(1)* **Auxiliary Space:** *O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**