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
Method
1) Compute a^b
2) Iteratively remove the last digit until k-th digit is not meet
C++
// CPP program for finding k-th digit in a^b #include <bits/stdc++.h> using namespace std; // To compute k-th digit in a^b int 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 code int main() { int a = 5, b = 2; int k = 1; cout << kthdigit(a, b, k); return 0; } |
Java
// Java program for finding k-th digit in a^b import 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
# Python3 code to compute k-th # digit in a^b def kthdigit(a, b, k): # computin 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#
// C# program for finding k-th digit in a^b using 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 // PHP 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 $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. ?> |
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Larger of a^b or b^a (a raised to power b or b raised to power a)
- Find the sum of power of bit count raised to the power B
- Find unit digit of x raised to power y
- Find last five digits of a given five digit number raised to power five
- Last digit of a number raised to last digit of N factorial
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Check if a number can be expressed as x^y (x raised to power y)
- Print last k digits of a^b (a raised to power b)
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find value of y mod (2 raised to power x)
- Number of digits in 2 raised to power n
- GCD of a number raised to some power and another number
- Minimum removals in a number to be divisible by 10 power raised to K
- Sum of absolute difference of all pairs raised to power K
- Count of N-digit numbers having digit XOR as single digit
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Count pairs in Array whose product is a Kth power of any positive integer
- Calculate Root Mean Kth power of all array elements
- Find power of power under mod of a prime
- Check if given number is a power of d where d is a power of 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m

