Kth digit from right of A^B

PROGRAM TO PRINT K-th DIGIT IN A^B FROM RIGHT SIDE


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 = 5
b = 2
k = 1
ans = kthdigit(a, b, k)
print (ans)


OUTPUT
5

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java