Online Compiler C

#include <stdio.h> int main() { int n = 0, k = 1; int t = n, cnt = 0; while(n) { cnt++; n/=10; } n = t; int endn = cnt - k; for(int i=0; i<endn; i++) { n /= 10; } printf("%d", n%10); return 0; }
1) This code attempts to find the k-th digit from the right of a number, but it has logical errors that prevent it from working correctly.

2) Hint: The initial value of `n` is 0, so the while loop that counts digits never executes. Also, consider what happens when `k` is larger than the number of digits in `n`.