TechoSagar: Print the Kth Digit

Print the Kth Digit

Given two numbers a and b, find kth digit from right of a^b.
Input:
The first line of the input contains T denoting the number of test cases.Each of the next T lines contains three space separated positive integers denoting the value of a , b and k respectively.

Output:
For each test case, output the kth digit from right of a^b in new line.

Constraints:
1<=T<=100
1<=a , b <=15
1<=k<=|totaldigits in a^b|

Example:
Input:
2
3 3 1
5 2 2
Output:
7
2
Your Code
#include<iostream>

using namespace std;
#include<math.h>

int main()
{
int t;
cin>>t;
while(t--)
{
int a,b,k;
cin>>a>>b>>k;;
long int c;
c=pow(a, b);
int i,digit;
for(i=0; i<k; i++)
{
digit=c%10;
c=c/10;
}
cout<<digit<<endl;
}
return 0;
}

Follow us

Follow Bijendra Kumar on Facebook