简体   繁体   English

如何找到 c 中 int digits 的位数最多为 100 或 1000 位?

[英]How to find the number of digits int digits in c upto 100 or 1000 digits?

This is my code:`这是我的代码:`

#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);  
}

I am able to run the code finely but when I enter 15 or 16 digits of number as input then it always shows me that the number of digits is 10 .我能够很好地运行代码,但是当我输入15 或 16 位数字作为输入时,它总是显示数字是10 And another problem with this code is that suppose if I input 000 then I want the output to be 3 digits but this code is not able to do that as the condition in the while loop becomes instantly false.这段代码的另一个问题是,假设如果我输入000 ,那么我希望 output 是3 digits ,但是这段代码无法做到这一点,因为while循环中的条件立即变为假。 So how write a code that enables me to take upto 100 or 1000 digits as input and also enables me to input 0s as well.那么如何编写一个代码,使我能够将多达 100 或 1000 位数字作为输入,同时也使我能够输入0

Note: This program should be solved using a loop and in C language I found a answer to the question here in stackoverflow 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++注意:这个程序应该使用循环来解决,在 C 语言中,我在 c++ 中写的 stackoverflow 中找到了问题的答案,我什至无法理解,因为我是初学者,我正在学习 C。链接到答案: 如何计算 C/C++ 中最多 1000 位数字的位数

You are taking int variable and you are trying to count a number like whose digit is 100 or 1000. it will not fit with int.您正在使用 int 变量,并且您正在尝试计算一个数字,例如其数字为 100 或 1000 的数字。它不适合 int。 so take input as a string and count the length of string.所以将输入作为字符串并计算字符串的长度。

Instead of reading a number, read a string and count the digits:不要读取数字,而是读取字符串并计算数字:

#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; 
}

You can also use the scanf() scanset feature to perform the test in one step:您还可以使用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 bits signed integer has the max value equals 2,147,483,647 so, if you input a bigger one, it will not be stored. 32 位有符号 integer 的最大值等于 2,147,483,647 所以,如果你输入一个更大的,它不会被存储。 I'd make it receiving a string and get its length, like so:我会让它接收一个字符串并获取它的长度,如下所示:

#include <stdio.h>
#include <string.h>

int len = strlen("123123123123132");

This is my code:`这是我的代码:`

#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);  
}

I am able to run the code finely but when I enter 15 or 16 digits of number as input then it always shows me that the number of digits is 10 .我能够很好地运行代码,但是当我输入15 或 16 位数字作为输入时,它总是显示我的位数是10 And another problem with this code is that suppose if I input 000 then I want the output to be 3 digits but this code is not able to do that as the condition in the while loop becomes instantly false.此代码的另一个问题是,假设如果我输入000 ,那么我希望 output 为3 digits ,但此代码无法做到这一点,因为while循环中的条件立即变为错误。 So how write a code that enables me to take upto 100 or 1000 digits as input and also enables me to input 0s as well.那么如何编写一个代码,使我能够输入多达 100 或 1000 位数字,同时也使我能够输入0

Note: This program should be solved using a loop and in C language I found a answer to the question here in stackoverflow written in c++ that I couldn't even understand as I am a beginner and I am learning C.注意:这个程序应该使用循环和 C 语言来解决,我在 stackoverflow 中找到了这个问题的答案,用 c++ 编写,我什至无法理解,因为我是初学者,我正在学习 Z0D61F8370CAD1D4D412F80B84D17。 Link to the answer: How can I count the number of digits in a number up to 1000 digits in C/C++链接到答案: 如何计算 C/C++ 中最多 1000 位的数字的位数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM