如何找到 c 中最多 100 或 1000 位数字的位数?
How to find the number of digits int digits in c upto 100 or 1000 digits?
这是我的代码:`
#include <stdio.h>
void main() {
int n;
int count = 0;
printf("Enter an integer: ");
scanf("%d", &n);
// iterate until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
while (n != 0) {
n /= 10; // n = n/10
++count;
}
printf("Number of digits: %lld", count);
}
我能够 运行 代码,但是当我输入 15 或 16 位数字 作为输入时它总是显示位数是 10
。这段代码的另一个问题是,假设如果我输入 000
那么我希望输出为 3 digits
但这段代码无法做到这一点,因为 while
循环中的条件变为瞬间虚假。那么如何编写一个代码,使我能够将 最多 100 或 1000 位数字作为输入 并使我能够输入 0s也是。
Note: This program should be solved using a loop and in C language
I found a answer to the question here in Whosebug written in c++ that I couldn't even understand as I am a beginner and I am learning C.
Link to the answer:
How can I count the number of digits in a number up to 1000 digits in C/C++
您正在使用 int 变量,并且您正在尝试计算一个数字,例如其数字为 100 或 1000 的数字。它不适合 int。所以将输入作为字符串并计算字符串的长度。
不是读取数字,而是读取字符串并计算数字:
#include <stdio.h>
int main() {
char buffer[10000];
int n;
printf("Enter an integer: ");
if (scanf("%9999s", buffer) == 1) {
for (n = 0; buffer[n] >= '0' && buffer[n] <= '9'; n++)
continue;
printf("Number of digits: %d\n", n);
}
return 0;
}
您也可以使用scanf()
扫描集功能一步完成测试:
#include <stdio.h>
int main() {
char buffer[10000];
int n;
printf("Enter an integer: ");
if (scanf("%9999[0-9]%n", buffer, &n) == 1) {
printf("Number of digits: %d\n", n);
}
return 0;
}
32 位有符号整数的最大值为 2,147,483,647,因此,如果您输入更大的整数,则不会被存储。我会让它接收一个字符串并得到它的长度,像这样:
#include <stdio.h>
#include <string.h>
int len = strlen("123123123123132");
这是我的代码:`
#include <stdio.h>
void main() {
int n;
int count = 0;
printf("Enter an integer: ");
scanf("%d", &n);
// iterate until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
while (n != 0) {
n /= 10; // n = n/10
++count;
}
printf("Number of digits: %lld", count);
}
我能够 运行 代码,但是当我输入 15 或 16 位数字 作为输入时它总是显示位数是 10
。这段代码的另一个问题是,假设如果我输入 000
那么我希望输出为 3 digits
但这段代码无法做到这一点,因为 while
循环中的条件变为瞬间虚假。那么如何编写一个代码,使我能够将 最多 100 或 1000 位数字作为输入 并使我能够输入 0s也是。
Note: This program should be solved using a loop and in C language I found a answer to the question here in Whosebug written in c++ that I couldn't even understand as I am a beginner and I am learning C. Link to the answer: How can I count the number of digits in a number up to 1000 digits in C/C++
您正在使用 int 变量,并且您正在尝试计算一个数字,例如其数字为 100 或 1000 的数字。它不适合 int。所以将输入作为字符串并计算字符串的长度。
不是读取数字,而是读取字符串并计算数字:
#include <stdio.h>
int main() {
char buffer[10000];
int n;
printf("Enter an integer: ");
if (scanf("%9999s", buffer) == 1) {
for (n = 0; buffer[n] >= '0' && buffer[n] <= '9'; n++)
continue;
printf("Number of digits: %d\n", n);
}
return 0;
}
您也可以使用scanf()
扫描集功能一步完成测试:
#include <stdio.h>
int main() {
char buffer[10000];
int n;
printf("Enter an integer: ");
if (scanf("%9999[0-9]%n", buffer, &n) == 1) {
printf("Number of digits: %d\n", n);
}
return 0;
}
32 位有符号整数的最大值为 2,147,483,647,因此,如果您输入更大的整数,则不会被存储。我会让它接收一个字符串并得到它的长度,像这样:
#include <stdio.h>
#include <string.h>
int len = strlen("123123123123132");