码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-03-21 01:50:11      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:求n的阶乘以及前n个数的阶乘和

//求n的阶乘

#include<stdio.h>

#include<stdlib.h>

int main()

{

                 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>

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;

}


本文出自 “11132019” 博客,请务必保留此出处http://11142019.blog.51cto.com/11132019/1753287

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

标签:求n的阶乘以及前n个数的阶乘和

原文地址:http://11142019.blog.51cto.com/11132019/1753287

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!