Given a number we have to print the kth digit of a number .lets suppose we have a number 128 the first digit is 8 second digit is 2 and third digit is 1.
Example:
input: num =45326 ,k=2
output:2
input:4325343, k=4
output:5
Logic:
find the remainder of number untlil we reached at kth places.
#include<iostream> using namespace std; int main() { int num,k; cout<<"enter the number "<<endl; cin>>num>>k; int rem; while(num>0&&k!=0) { rem=num%10; num=num/10; k--; } cout<<rem<<endl; }
Output:
enter the number :2345355 5 4
Time Complexity:O(N)
space complexity:O(1)