Print the Kth Digit | Practice | GeeksforGeeks

Given two numbers A and B, find Kth digit from right of AB.
 
Example 1:
Input:
A = 3
B = 3
K = 1
Output:
7
Explanation:
33 = 27 and 1st
digit from right is 
7

Example 2:
Input:
A = 5
B = 2
K = 2
Output:
2
Explanation:
52 = 25 and second
digit from right is
2.


Your Task:
You don't need to read input or print anything. Your task is to complete the function kthDigit() which takes integers A, B, K as input parameters and returns Kth Digit of AB from right side.
 
Expected Time Complexity: O(log AB)
Expected Space Complexity: O(1)
 
Constraints:
1 <= A,B <= 15
1 <=K<= digits in AB
 
0
sakesai305 days ago
class Solution{   static long kthDigit(int A,int B,int K){      long result = (long)Math.pow(A,B);      if(K==1) return result%10;      long mama = result/((long)Math.pow(10,K-1));      return mama%10;   }}
0
shivamranageu20236 days ago
class Solution{public:   int kthDigit(int A,int B,int K){       // code here       long long int b= pow(A,B);       long long int c = pow(10,K);       return (int)((b%c)/(c/10));   }};
0
mohdraza2 weeks ago
class Solution{   static long kthDigit(int A,int B,int K){       // code here       long a =(long)Math.pow(A,B);       String st=Long.toString(a);       long ch=st.charAt(st.length()-K)-'0';       return ch;   }}
+1
rapuriteja3 weeks ago
def kthDigit(self, A, B, K):       num = A**B       return str(num)[-K]
0
mayank180919994 weeks ago
   int kthDigit(int A,int B,int K){
        // code her
        long long int N=pow(A,B);
        int digit;
        while(K>0){
            digit=N%10;
            N=N/10;
            K--;
        }
      return digit;
    }
0
giridharsonu1231 month ago
JavaScript Solution       let n=0;       let rem=0;       let ans=Math.pow(A,B);       while(ans > 0){          n++;          rem=ans%10;          if(K==n){              return Math.floor(rem);          }          ans/=10;   } 
0
asifsaba5812 months ago
class Solution{
public:
    int kthDigit(int A,int B,int K){
        
        long long int  value=pow(A,B);
        
     long long int  count=0;
     
     long long int  k_th_Digit=0;
     
       while(value>0){
           
           k_th_Digit=value%10;
           
           count++;
           
           if(count==K){
               
               return k_th_Digit;
           }
           
           value=value/10;
       }
       
       return 0; 
    }
    
    
};
0
bhanujggandhi2 months ago
Using pow() function in C++
int kthDigit(int A,int B,int K){
        long long int num = pow(A, B);
        
        long long int truncate_num = num/pow(10,K-1);
        return truncate_num%10;
    }
0
gupta2411sumit2 months ago
int kthDigit(int A,int B,int K){       // code here              long long int num = pow( A , B ) ;              //        int ans = 0 ;             while(K>0)       {           ans = num%10 ;           num = num/10 ;           K-- ;       }              return ans ;                 }
0
chechipresh2 months ago
class Solution{
public:
    int kthDigit(int A,int B,int K){
        // code here 
        long int p=pow(A,B);
        int d;
        while (1){
            d=p%10;
            K--;
            if (K==0)
            return d;
            p/=10;
        }
    };
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.