求n的阶乘以及前n个数的阶乘和

//求n的阶乘
#include<stdio.h>
#include<stdlib.h>
intmain()
{
                int n = 0;
                int sum = 1;
                scanf("%d", &n);
                while (n)                             //n如果为零,则不进入循环体,输出的sum初始值就是1
                {
                                sum *=n;
                                n--;
                }
                printf("%d\n", sum);
                system("pause");
                return 0;
}


//求从1到n的阶乘和
#include<stdio.h>
#include<stdlib.h>
intmain()
{
                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;
}
posted @ 2016-03-20 21:41  午饭要阳光  阅读(306)  评论(0编辑  收藏  举报