The Wayback Machine - https://web.archive.org/web/20250304102054/https://www.geeksforgeeks.org/k-th-digit-raised-power-b/
Open In App

K-th digit in ‘a’ raised to power ‘b’

Last Updated : 19 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.
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));
    }
    
}
Python
# Python3 code to compute k-th 
# digit in a^b
def 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 = 5
b = 2
k = 1
ans = kthdigit(a, b, k)
print (ans)
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));
    }
    
}
JavaScript
// 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;
        console.log(kthdigit(a, b, k));   
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);

?>

Output
5

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



Next Article

Similar Reads

three90RightbarBannerImg