C语言乘阶如何写?

来源:百度知道 编辑:UC知道 时间:2024/06/01 02:56:21
#include <stdio.h>

int main (void)
{
int n, rusult;

rusult = 1;

for ( n = 1; n <= 10; ++n ) {
printf ("%2i %i\n", n, rusult);
rusult = n * rusult;
}

return 0;
}

这样写总是不对,请高手讲讲应该怎样写?
呵呵,我也正好想到了这样的。
可是算出来的乘阶怎么还有负数哦,好像也不对?
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 -25216
9 -30336
10 24320

#include <stdio.h>

int main (void)
{
unsigned long n, rusult; //这样就不会超范围了
rusult = 1;
for ( n = 1; n <= 10; ++n ) {
rusult = n * rusult;
printf ("%2lu %lu\n", n, rusult);
}
return 0;
}

printf ("%2i %i\n", n, rusult);
rusult = n * rusult;
这两句顺序倒过来就对了,即改为:
rusult = n * rusult;
printf ("%2i %i\n", n, rusult);