Find unit digit of x raised to power y

23 Jun, 2022
Given two numbers x and y, find unit digit of xy.
Examples : 
Input  : x = 2, y = 1
Output : 2
Explanation
2^1 = 2 so units digit is 2.

Input : x = 4, y = 2
Output : 6
Explanation
4^2 = 16 so units digit is 6.
Method 1 (Simple) Compute value of xy and find its last digit. This method causes overflow for slightly larger values of x and y.Method 2 (Efficient) 1) Find last digit of x. 2) Compute x^y under modulo 10 and return its value. 
C++
Java
Python3
C#
PHP
Javascript
// Efficient C++ program to// find unit digit of x^y.#include <bits/stdc++.h>using namespace std; // Returns unit digit of x// raised to power yint unitDigitXRaisedY(int x, int y){    // Initialize result as 1 to    // handle case when y is 0.    int res = 1;     // One by one multiply with x    // mod 10 to avoid overflow.    for (int i = 0; i < y; i++)        res = (res * x) % 10;     return res;} // Driver programint main(){    cout << unitDigitXRaisedY(4, 2);   return 0;}
// Efficient Java program to find// unit digit of x^y.import java.io.*; class GFG {    // Returns unit digit of x raised to power y    static int unitDigitXRaisedY(int x, int y)    {        // Initialize result as 1 to        // handle case when y is 0.        int res = 1;             // One by one multiply with x        // mod 10 to avoid overflow.        for (int i = 0; i < y; i++)            res = (res * x) % 10;             return res;    }         // Driver program    public static void main(String args[])throws IOException    {    System.out.println(unitDigitXRaisedY(4, 2));    }} // This code is contributed by Nikita Tiwari.
# Python3 code to find# unit digit of x^y. # Returns unit digit of# x raised to power ydef unitDigitXRaisedY( x , y ):     # Initialize result as 1 to    # handle case when y is 0.    res = 1         # One by one multiply with x    # mod 10 to avoid overflow.    for i in range(y):        res = (res * x) % 10         return res     # Driver programprint( unitDigitXRaisedY(4, 2))  # This code is contributed by Abhishek Sharma44.
// Efficient Java program to find// unit digit of x^y.using System; class GFG{    // Returns unit digit of x raised to power y    static int unitDigitXRaisedY(int x, int y)    {        // Initialize result as 1 to        // handle case when y is 0.        int res = 1;             // One by one multiply with x        // mod 10 to avoid overflow.        for (int i = 0; i < y; i++)            res = (res * x) % 10;             return res;    }         // Driver program    public static void Main()    {    Console.WriteLine(unitDigitXRaisedY(4, 2));    }} // This code is contributed by vt_m.
<?php// Efficient PHP program to// find unit digit of x^y. // Returns unit digit of x// raised to power yfunction unitDigitXRaisedY($x, $y){    // Initialize result as 1 to    // handle case when y is 0.    $res = 1;     // One by one multiply with x    // mod 10 to avoid overflow.    for ($i = 0; $i < $y; $i++)        $res = ($res * $x) % 10;     return $res;} // Driver Codeecho(unitDigitXRaisedY(4, 2)); // This code is contributed by Ajit.?>
<script> // Efficient Javascript program to// find unit digit of x^y. // Returns unit digit of x// raised to power yfunction unitDigitXRaisedY(x, y){         // Initialize result as 1 to    // handle case when y is 0.    let res = 1;     // One by one multiply with x    // mod 10 to avoid overflow.    for(let i = 0; i < y; i++)        res = (res * x) % 10;     return res;} // Driver Codedocument.write(unitDigitXRaisedY(4, 2)); // This code is contributed by _saurabh_jaiswal. </script>
Output : 
6
Time Complexity: O(y), where y is the powerAuxiliary Space: O(1), as no extra space is required
Further Optimizations: We can compute modular power in Log y.
Method 3 (Direct based on cyclic nature of last digit) This method depends on the cyclicity with the last digit of x that is
x   |  power 2  |  power 3  |   power 4  | Cyclicity  
0   | .................................. |  .... repeat with 0
1   | .................................. |  .... repeat with 1
2   |     4     |     8     |      6     | .... repeat with 2
3   |     9     |     7     |      1     | .... repeat with 3
4   |     6     |....................... |  .... repeat with 4
5   | .................................. |  .... repeat with 5
6   | .................................. |  .... repeat with 6
7   |     9     |     3     |      1     | .... repeat with 7
8   |     4     |     2     |      6     | .... repeat with 8
9   |     1     | ...................... |  .... repeat with 9 
So here we directly mod the power y with 4 because this is the last power after this all number’s repetition start after this we simply power with number x last digit then we get the unit digit of produced number. 
C++
Java
Python3
C#
PHP
Javascript
// C++ code to find the unit digit of x// raised to power y.#include<iostream>#include<math.h>using namespace std; // find unit digitint unitnumber(int x, int y){    // Get last digit of x    x = x % 10;           // Last cyclic modular value    if(y!=0)        y = y % 4 + 4;     // here we simply return the    // unit digit or the power    // of a number    return (((int)(pow(x, y))) % 10);} int main(){    int x = 133, y = 5;         // get unit digit number here we pass    // the unit  digit of x and the last    // cyclicity number that is y%4    cout << unitnumber(x, y);      return 0;}
// Java code to find the unit// digit of x  raised to power y.import java.io.*;import java.util.*; class GFG {         // find unit digit    static int unitnumber(int x, int y)    {        // Get last digit of x        x = x % 10;                     // Last cyclic modular value        if(y!=0)             y = y % 4 + 4;             // here we simply return the        // unit digit or the power        // of a number        return (((int)(Math.pow(x, y))) % 10);    }              public static void main (String[] args)    {        int x = 133, y = 5;             // get unit digit number here we pass        // the unit digit of x and the last        // cyclicity number that is y%4        System.out.println(unitnumber(x, y));         }} // This code is contributed by Gitanjali.
# Python3 code to find the unit  # digit of x raised to power y.import math # Find unit digitdef unitnumber(x, y):     # Get last digit of x    x = x % 10             # Last cyclic modular value    if y!=0:         y = y % 4 + 4     # Here we simply return     # the unit digit or the     # power of a number    return (((int)(math.pow(x, y))) % 10)  # Driver codex = 133; y = 5     # Get unit digit number here we pass# the unit digit of x and the last# cyclicity number that is y%4print(unitnumber(x, y))  # This code is contributed by Gitanjali.
// C# code to find the unit// digit of x raised to power y.using System; class GFG {         // find unit digit    static int unitnumber(int x, int y)    {        // Get last digit of x        x = x % 10;                     // Last cyclic modular value        if(y!=0)             y = y % 4 + 4;             // here we simply return the        // unit digit or the power        // of a number        return (((int)(Math.Pow(x, y))) % 10);    }         // Driver code    public static void Main ()    {        int x = 133, y = 5;             // get unit digit number here we pass        // the unit digit of x and the last        // cyclicity number that is y%4        Console.WriteLine(unitnumber(x, y));         }} // This code is contributed by vt_m.
<?php// PHP code to find the unit// digit of x raised to power y. // find unit digitfunction unitnumber($x, $y){    // Get last digit of x    $x = $x % 10;         // Last cyclic modular value    if($y!=0)        $y = $y % 4 + 4;     // here we simply return the    // unit digit or the power    // of a number    return (((int)(pow($x, $y))) % 10);} // Driver code$x = 133; $y = 5;     // get unit digit number here we pass// the unit digit of x and the last// cyclicity number that is y%4echo(unitnumber($x, $y)); // This code is contributed by Ajit.?>
<script> // Javascript code to find the unit// digit of x raised to power y. // find unit digitfunction unitnumber(x, y){         // Get last digit of x    x = x % 10;         // Last cyclic modular value    if (y != 0)        y = y % 4 + 4;     // here we simply return the    // unit digit or the power    // of a number    return ((parseInt(Math.pow(x, y))) % 10);} // Driver codelet x = 133;let y = 5;     // get unit digit number here we pass// the unit digit of x and the last// cyclicity number that is y%4document.write(unitnumber(x, y)); // This code is contributed by _saurabh_jaiswal. </script>
Output : 
3
Time Complexity: O(log n)Auxiliary Space: O(1)
Thanks to DevanshuAgarwal for suggesting above solution.How to handle large numbers? Efficient method for Last Digit Of a^b for Large Numbers 
jit_t
gowtham_yuvaraj
_saurabh_jaiswal
singhh3010
Modular Arithmetic
Mathematical
Mathematical
Modular Arithmetic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.