VOOZH about

URL: https://www.geeksforgeeks.org/dsa/k-th-digit-raised-power-b/

⇱ K-th digit in 'a' raised to power 'b' - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

K-th digit in 'a' raised to power 'b'

Last Updated : 3 Jun, 2025

Given three numbers a, b and k, find k-th digit in ab from right side

Examples: 

Input : a = 3, b = 3, k = 1
Output : 7
Explanation: 3^3 = 27 for k = 1. First digit is 7 in 27

Input : a = 5, b = 2,  k = 2
Output : 2
Explanation: 5^2 = 25 for k = 2. First digit is 2 in 25

The 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.

Output
5

Time Complexity: O(log b)
Auxiliary Space: O(1)

Comment