Find Last Digit of a^b for Large Numbers (original) (raw)

Last Updated : 2 May, 2026

You are given two integers a and b in the form of strings. You need to find the last digit of **a^b (ab).

Examples:

**Input: a = "3", b = "10"
**Output: 9
**Explanation: 310 = 59049. The last digit is 9.

**Input: a = "6", b = "2"
**Output: 6
**Explanation: 62 = 36. The last digit is 6.

Try It Yourselfredirect icon

Using Cyclicity of Last Digit (Mod 10) - O(n) Time O(1) Space

The last digit of powers follows a repeating cycle (maximum length = 4). Instead of computing large powers, we reduce the exponent using modulo 4 and use only the last digit of the base.

Here are few examples numbers and last digits of their powers

**Algorithm:

**Why does this work?

// C++ code to find last digit of a^b #include <bits/stdc++.h> using namespace std;

int Modulo(int a, string b) { int mod = 0;

// calculate (b mod a) to make
// b in range 0 <= b < a
for (int i = 0; i < b.length(); i++)
    mod = (mod * 10 + b[i] - '0') % a;

return mod;

}

int getLastDigit(string a, string b) {

int len_a = a.length(), len_b = b.length();

// exponent is 0
if (len_b == 1 && b[0] == '0')
    return 1;

// base is 0
if (len_a == 1 && a[0] == '0')
    return 0;

// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
int exp = (Modulo(4, b) == 0) ? 4 : Modulo(4, b);
int res = pow(a[len_a - 1] - '0', exp);
return res % 10;

}

// Driver program to run test case int main() { char a[] = "3", b[] = "10"; cout << getLastDigit(a, b); return 0; }

Java

import java.util.*;

public class GfG { int Modulo(int a, String b) { int mod = 0;

    // calculate (b mod a) to make
    // b in range 0 <= b < a
    for (int i = 0; i < b.length(); i++)
        mod = (mod * 10 + b.charAt(i) - '0') % a;

    return mod;
}

int getLastDigit(String a, String b) {

    int len_a = a.length(), len_b = b.length();

    // exponent is 0
    if (len_b == 1 && b.charAt(0) == '0')
        return 1;

    // base is 0
    if (len_a == 1 && a.charAt(0) == '0')
        return 0;

    // if exponent is divisible by 4 that means last
    // digit will be pow(a, 4) % 10
    // otherwise last digit will be pow(a, b%4) % 10
    int exp = (Modulo(4, b) == 0) ? 4 : Modulo(4, b);
    int res = (int)Math.pow(a.charAt(len_a - 1) - '0', exp);
    return res % 10;
}

public static void main(String[] args) {
    GfG m = new GfG();
    String a = "3", b = "10";
    System.out.println(m.getLastDigit(a, b));
}

}

Python

def Modulo(a, b): mod = 0

# calculate (b mod a) to make
# b in range 0 <= b < a
for i in range(len(b)):
    mod = (mod * 10 + int(b[i])) % a

return mod

def getLastDigit(a, b):

len_a = len(a)
len_b = len(b)

# exponent is 0
if len_b == 1 and b[0] == '0':
    return 1

# base is 0
if len_a == 1 and a[0] == '0':
    return 0

# if exponent is divisible by 4 that means last
# digit will be pow(a, 4) % 10
# otherwise last digit will be pow(a, b%4) % 10
exp = 4 if Modulo(4, b) == 0 else Modulo(4, b)
res = pow(int(a[len_a - 1]), exp)
return res % 10

if name == "main": a = "3" b = "10" print(getLastDigit(a, b))

C#

using System;

public class GfG { public int Modulo(int a, string b) { int mod = 0;

    // calculate (b mod a) to make
    // b in range 0 <= b < a
    for (int i = 0; i < b.Length; i++)
        mod = (mod * 10 + b[i] - '0') % a;

    return mod;
}

public int GetLastDigit(string a, string b)
{
    int len_a = a.Length, len_b = b.Length;

    // if a and b both are 0
    if (len_a == 1 && len_b == 1 && a[0] == '0' && b[0] == '0')
        return 1;

    // exponent is 0
    if (len_b == 1 && b[0] == '0')
        return 1;

    // base is 0
    if (len_a == 1 && a[0] == '0')
        return 0;

    int mod = Modulo(4, b);
    int exp = (mod == 0) ? 4 : mod;

    int res = (int)Math.Pow(a[len_a - 1] - '0', exp);
    return res % 10;
}

public static void Main()
{
    GfG p = new GfG();
    string a = "3", b = "10";
    Console.WriteLine(p.GetLastDigit(a, b));
}

}

JavaScript

function Modulo(a, b) { let mod = 0;

// calculate (b mod a) to make
// b in range 0 <= b < a
for (let i = 0; i < b.length; i++) {
    mod = (mod * 10 + parseInt(b[i])) % a;
}

return mod;

}

function getLastDigit(a, b) {

let len_a = a.length, len_b = b.length;

// exponent is 0
if (len_b == 1 && b[0] == '0')
    return 1;

// base is 0
if (len_a == 1 && a[0] == '0')
    return 0;

// if exponent is divisible by 4 that means last
// digit will be pow(a, 4) % 10
// otherwise last digit will be pow(a, b%4) % 10
let exp = (Modulo(4, b) == 0)? 4 : Modulo(4, b);
let res = Math.pow(parseInt(a[len_a - 1]), exp);
return Math.floor(res % 10);

}

// Driver program to run test case let a = "3", b = "10"; console.log(getLastDigit(a, b));

`

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