K-th digit in 'a' raised to power 'b' Last Updated : 03 Jun, 2025 Comments Improve Suggest changes 2 Likes Like Report Try it on GfG Practice Given three numbers a, b and k, find k-th digit in ab from right sideExamples: Input : a = 3, b = 3, k = 1Output : 7Explanation: 3^3 = 27 for k = 1. First digit is 7 in 27Input : a = 5, b = 2, k = 2Output : 2Explanation: 5^2 = 25 for k = 2. First digit is 2 in 25The approach is simple. Computes the value of ab Then iterates through the digits of the result. Starting from the rightmost digit, it extracts each digit one by one by using the modulus operation (p % 10). The extracted digit is compared with the desired position k. It continues removing the last digit (using integer division by 10) until it reaches the k-th digit, which is then returned. C++ #include <bits/stdc++.h> using namespace std; // Modular exponentiation to compute (a^b) % (10^k) int kthDigit(int a, int b, int k){ long long mod = pow(10LL, k); long long res = 1; long long base = a; while (b > 0) { if (b & 1) { res = (res * base) % mod; } base = (base * base) % mod; b >>= 1; } for (int i = 1; i < k; i++) res /= 10; return (int)(res); } // Driver code int main(){ int a = 5, b = 2; int k = 1; cout << kthDigit(a, b, k); return 0; } Java public class Main { static int kthDigit(int a, int b, int k) { long mod = (long) Math.pow(10, k); long res = 1; long base = a; while (b > 0) { if ((b & 1) == 1) { res = (res * base) % mod; } base = (base * base) % mod; b >>= 1; } for (int i = 1; i < k; i++) res /= 10; return (int)res; } public static void main(String[] args) { int a = 5, b = 2, k = 1; System.out.println(kthDigit(a, b, k)); } } Python def kthDigit(a, b, k): mod = 10 ** k res = 1 base = a while b > 0: if b & 1: res = (res * base) % mod base = (base * base) % mod b >>= 1 for i in range(1,k): res //= 10 return res # Driver code if __name__ == "__main__": a = 5 b = 2 k = 1 print(kthDigit(a, b, k)) C# using System; class GfG{ static int kthDigit(int a, int b, int k){ long mod = (long)Math.Pow(10, k); long res = 1; long baseVal = a; while (b > 0){ if ((b & 1) == 1){ res = (res * baseVal) % mod; } baseVal = (baseVal * baseVal) % mod; b >>= 1; } for (int i = 1; i < k; i++) res /= 10; return (int)(res); } static void Main(){ int a = 5, b = 2, k = 1; Console.WriteLine(kthDigit(a, b, k)); } } JavaScript function kthDigit(a, b, k) { let mod = 10 ** k; let res = 1; let base = a % mod; while (b > 0) { if (b & 1) { res = (res * base) % mod; } base = (base * base) % mod; b >>= 1; } for (let i = 1; i < k; i++) { res = Math.floor(res / 10); } return res; } // Example usage: let a = 5, b = 2, k = 1; console.log(kthDigit(a, b, k)); // Output: 5 Output5Time Complexity: O(log b)Auxiliary Space: O(1) Comment S Shahnawaz_Ali Follow 2 Improve S Shahnawaz_Ali Follow 2 Improve Article Tags : Misc Mathematical DSA Flipkart number-digits +1 More Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like