[英]How do i get third digit from right of any given number in cpp?
我如何獲得這樣的任何給定數字的第三位數字
Input Output
1234567 5
98765 7
我已經試過這個功能
char nthdigit(int x, int n)
{
while (n--) {
x /= 10;
}
return (x % 10) + '0';
}
但這對我來說不合適
按照您想要實現的目標,您的n--
實際上應該是--n
char nthdigit(int x, int n)
{
while (--n) {
x /= 10;
}
return (x % 10) + '0';
}
然后,您將得到所需的內容: nthdigit(1234567, 3)
返回5; nthdigit(98765, 3)
返回7; 雖然這不是優化的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.