taeyounkim LOG

function that returns the k(th) digit(from the right) in n(a positive integer) 본문

Career&Study/Coding Practice

function that returns the k(th) digit(from the right) in n(a positive integer)

taeyounkim 2021. 6. 30. 12:43
728x90

#include <stdio.h>

int digit(int n, int k)
{
  
    while(k>1){
        n/=10;
        k--;
    }
    
    return n%10;
}

int main(){
    int n, k;
    
    printf("Enter n, k: ");
    scanf("%d %d", &n, &k);
    
    printf("%d", digit(n,k));
    return 0;
}

728x90