如何使用c语言编程输入整数n,求1+2+3+……+n和n!?
1. 使用c语言编程输入整数n,求1+2+3+……+n和n!
#include<stdio.h>
#include<math.h>
int main()
{
int i;
int nNumber;
int nSum = 0;
printf("Input Data:'N'");
scanf("%d", &nNumber);
for ( i = 1; i <= nNumber; i++)
{
nSum = nSum + i;
}
printf("%d\r\n", nSum);
return 0;
}
2. 使用c语言编程输入整数n,求1+2+3+……+n和n!
#include<stdio.h>
#include<math.h>
int Factorial(int n);
int Add(int n);
int main()
{
printf("阶乘:%d\n",Factorial(10));
printf("求和:%d\n",Add(10));
}
int Factorial(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*JieCheng(n-1);
}
}
int Add(int n){
int i;
int s=0;
for(i=1;i<=n;i++)
{
s+=i;
}
return s;
}
12345678910111213141516171819202122232425262728293031323334#include<stdio.h>#include<math.h> int JieCheng(int n);int QiuHe(int n); int main(){ printf("<a href="http://www.baidu.com/s?wd=%E9%98%B6%E4%B9%98&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHcvrjTdrjb0T1Yvry79nAw-n1wBP16sPHcL0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWcLrjDsP1RLP1n3nHDzrHcY" target="_blank" class="baidu-highlight">阶乘</a>:%d\n",JieCheng(10)); printf("求和:%d\n",QiuHe(10));}//计算n!int JieCheng(int n){ if(n==1) { return 1; } else { return n*JieCheng(n-1); }}//求和,1+2+3+<a href="http://www.baidu.com/s?wd=%E2%80%A6%E2%80%A6&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHcvrjTdrjb0T1Yvry79nAw-n1wBP16sPHcL0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPWcLrjDsP1RLP1n3nHDzrHcY" target="_blank" class="baidu-highlight">……</a>+nint QiuHe(int n){ int i; int s=0; for(i=1;i<=n;i++) { s+=i; } return s;}
这样行么。