gpt4 book ai didi

c - 如何在C中输入非常大的数字(超过200位)?

转载 作者:行者123 更新时间:2023-11-30 16:56:19 27 4
gpt4 key购买 nike

我想在C中输入非常大的数字,并且我还想计算其数字之和。有没有办法输入非常大的数字?

这是我的代码。

#include<stdio.h>
main() {
int sum=0,rem;
int a;
printf("Enter a number:-");
scanf("%d",a);
}

最佳答案

您可能不想将所有这 200 位数字加载到内存中。当您想要计算的只是数字之和时,那么在程序中您所需要的只是一些存储到目前为止数字之和的累加器变量。该变量的类型可以是 int ,因为200 * 9 <= INT_MAX在 C 的一致性实现中始终如此。

#include <stdio.h>

int main(void) {
int accumulator = 0;
int read_result;

printf("Enter a number:-");

while (1) {
read_result = fgetc(stdin);

if ('0' <= read_result && read_result <= '9') {
accumulator += read_result - '0';
} else {
break;
}
}

printf("The sum of the digits is %d", accumulator);

return 0;
}

关于c - 如何在C中输入非常大的数字(超过200位)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39943718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com