输入n 输出n的阶乘c语言程序,编一程序求n的阶乘(n由键盘输入)

该博客展示了一个C语言实现的阶乘计算程序,能够计算2000的阶乘。程序通过动态存储大整数并处理进位来完成计算,最终将结果输出到字符数组中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2000的阶乘都能算

2afc78f9bef3cdc1e4cbaf67c31529a7.png

#include

#include

#include

void factorial(int n, char *pout) {

if (pout == NULL) {

return;

}

int arr[20001];

int idx = 1;

arr[0] = 1;

for (int i = 1; i <= n; i++) {

int cry = 0;

for (int j = 0; j < idx; j++) {

arr[j] = arr[j] * i + cry;

cry = arr[j] / 10;

arr[j] %= 10;

}

while (cry > 10) {

arr[idx++] = cry % 10;

cry /= 10;

}

if (cry > 0)

arr[idx++] = cry;

}

for (int i = idx - 1; i >= 0; i--) {

*pout++ = arr[i] + '0';

}

*pout = '\0';

return;

}

int main() {

char buff[20000] = {0};

int n = 0;

scanf("%d", &n);

factorial(n, buff);

puts(buff);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值