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

17 Dec, 2021
Given three numbers a, b and k, find k-th digit in ab from right sideExamples: 
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
 
Method 1) Compute a^b 2) Iteratively remove the last digit until k-th digit is not meet 
C++
Java
Python3
C#
PHP
Javascript
// CPP program for finding k-th digit in a^b#include <bits/stdc++.h>using namespace std; // To compute k-th digit in a^bint kthdigit(int a, int b, int k){    // computing a^b    int p = pow(a, b);     int count = 0;    while (p > 0 && count < k) {         // getting last digit        int rem = p % 10;         // increasing count by 1        count++;         // if current number is required digit        if (count == k)            return rem;         // remove last digit        p = p / 10;    }     return 0;} // Driver codeint main(){    int a = 5, b = 2;    int k = 1;    cout << kthdigit(a, b, k);    return 0;}
// Java program for finding k-th digit in a^bimport java.util.*;import java.lang.*; public class GfG {    // To compute k-th digit in a^b    public static int kthdigit(int a, int b, int k)    {        // Computing a^b        int p = (int)Math.pow(a, b);         int count = 0;        while (p > 0 && count < k) {             // Getting last digit            int rem = p % 10;             // Increasing count by 1            count++;             // If current number is required digit            if (count == k)                return rem;             // Remove last digit            p = p / 10;        }         return 0;    }         // Driver Code    public static void main(String argc[]) {        int a = 5, b = 2;        int k = 1;        System.out.println(kthdigit(a, b, k));    }     } // This code is contributed by Sagar Shukla.
# Python3 code to compute k-th# digit in a^bdef kthdigit(a, b, k):         # computing a^b in python    p = a ** b    count = 0         while (p > 0 and count < k):                 # getting last digit        rem = p % 10         # increasing count by 1        count = count + 1         # if current number is        # required digit        if (count == k):            return rem         # remove last digit        p = p / 10;     # driver code   a = 5b = 2k = 1ans = kthdigit(a, b, k)print (ans) # This code is contributed by Saloni Gupta
// C# program for finding k-th digit in a^busing System; public class GfG {         // To compute k-th digit in a^b    public static int kthdigit(int a, int b, int k)    {        // Computing a^b        int p = (int)Math.Pow(a, b);         int count = 0;        while (p > 0 && count < k) {             // Getting last digit            int rem = p % 10;             // Increasing count by 1            count++;             // If current number is required digit            if (count == k)                return rem;             // Remove last digit            p = p / 10;        }         return 0;    }         // Driver Code    public static void Main() {        int a = 5, b = 2;        int k = 1;        Console.WriteLine(kthdigit(a, b, k));    }     } // This code is contributed by vt_m.
<?php// PHP program for finding// k-th digit in a^b // To compute k-th// digit in a^bfunction kthdigit($a, $b, $k){         // computing a^b    $p = pow($a, $b);     $count = 0;    while ($p > 0 and $count < $k)    {         // getting last digit        $rem = $p % 10;         // increasing count by 1        $count++;         // if current number is        // required digit        if ($count == $k)            return $rem;         // remove last digit        $p = $p / 10;    }     return 0;}     // Driver Code    $a = 5;    $b = 2;    $k = 1;    echo kthdigit($a, $b, $k); // This code is contributed by anuj_67.?>
<script> // JavaScript program for finding k-th digit in a^b // To compute k-th digit in a^b    function kthdigit(a, b, k)    {        // Computing a^b        let p = Math.pow(a, b);           let count = 0;        while (p > 0 && count < k) {               // Getting last digit            let rem = p % 10;               // Increasing count by 1            count++;               // If current number is required digit            if (count == k)                return rem;               // Remove last digit            p = p / 10;        }           return 0;    }        // Driver code                 let a = 5, b = 2;        let k = 1;        document.write(kthdigit(a, b, k));   </script>
Output: 
5
How to avoid overflow? We can find power under modulo 10sup>k to avoid overflow. After finding the power under modulo, we need to return first digit of the power.
vt_m
splevel62
varshagumber28
Flipkart
number-digits
Mathematical
Flipkart
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Print all possible combinations of r elements in a given array of size n
Program for factorial of a number
The Knight's tour problem | Backtracking-1
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array
Minimum number of jumps to reach end
Program to print prime numbers from 1 to N.