如何手动获取数字的第二位数字(How can I get 2nd digit of a number manually)

编程入门 行业动态 更新时间:2024-10-19 19:43:10
如何手动获取数字的第二位数字(How can I get 2nd digit of a number manually) #include <stdio.h> int main(int argc, char *argv[]) { int n = 234; // Gets first digit int digit = n / 100; putchar('0' + digit); // gets 3rd digit digit = n % 10; putchar('0' + digit); }

我怎样才能得到“3”又名第二位?

另外,如果n类似于245836,我怎么能逐一提取每个数字呢?

#include <stdio.h> int main(int argc, char *argv[]) { int n = 234; // Gets first digit int digit = n / 100; putchar('0' + digit); // gets 3rd digit digit = n % 10; putchar('0' + digit); }

How can I get "3" aka 2nd digit?

Also If the n was something like 245836 how could I extract each digit one by one?

最满意答案

您可以通过除以10然后将除法除数除以10来实现:

int second = (number / 10) % 10;

一般来说,你可以把整数除以k的十次幂除以k最低有效数字。 将剩余除数除以k的十次幂相当于保持最后的k位数。

我怎么能一个一个地提取每个数字?

您可以使用%10取最后一位数字,然后将该数字从结尾删除/= 10 。

You can do it by dividing by ten and then taking the remainder of division by ten:

int second = (number / 10) % 10;

In general, you could think of integer-dividing by a k-th power of ten as of dropping k least significant digits. Taking a remainder of division by k-th power of ten is equivalent to keeping the last k digits.

how could I extract each digit one by one?

You can take the last digit with %10 and then dropping that digit off the end by /= 10.

更多推荐

本文发布于:2023-08-06 21:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455730.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   digit   manually   number

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!