0029 编写一个函数digit(n,k),它回送n的从右边开始的第k个数字的值,例如digit(25469,3)=4, digit(724,4)=0


问题描述:

  编写一个函数digit(n,k),它回送n的从右边开始的第k个数字的值,例如digit(25469,3)=4, digit(724,4)=0

代码展示:

 1 #include
 2 int digit(int num, int x);    //num表示待查找的数,x表示从右往左第x位 
 3 int main(){
 4     int num,x;
 5     int result; 
 6     printf("请输入数字num与x:");
 7     scanf("%d %d",&num,&x);
 8     result = digit(num, x);
 9     printf("%d的从右往做第%d位是:%d\n", num, x, result);
10     return 0;
11 }
12 int digit(int num, int x){
13     int temp;
14     int i;
15     for(i=0; i){
16         temp = num % 10;
17         num = num /10;
18     }
19     return temp;
20 }

运行截图:

相关