求n的階乘以及前n個數的階乘和

//求n的階乘ide

#include<stdio.h>spa

#include<stdlib.h>it

int main()io

{class

                 int n = 0;循環

                 int sum = 1;im

                scanf( "%d", &n);di

                 while (n)                             //n若是爲零,則不進入循環體,輸出的sum初始值就是1view

                {vi

                                sum *=n;

                                n--;

                }

                printf( "%d\n", sum);

                system( "pause");

                 return 0;

}



//求從1到n的階乘和

#include<stdio.h>

#include<stdlib.h>

int main()

{

                 int n = 0;

                 int ret = 1;

                 int sum = 0;

                scanf( "%d", &n);

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

                {

                                ret *= i;                             //ret是一直沒有釋放,從1的階乘變到n的階乘

                                sum += ret;

                }

                printf( "%d\n",sum);

                system( "pause");

                 return 0;

}

相關文章
相關標籤/搜索