tfsf.net
当前位置:首页>>关于c语言编程从键盘输入一个数n(n>1),求n!的资料>>

c语言编程从键盘输入一个数n(n>1),求n!

long jiecheng(long n){ long a=1; if(n==0){ return 1; } else if(n>0){ return n*=jiecheng(n-1); } }

源程序如下: #include <stdio.h> int main() { int n,i,j,sum=1,t; scanf("%d", &n); for(i=1;i<=n;i++) { t=1; for(j=1;j<=i;j++) { t *= 2; //每次计算2 ^ i 出来 } sum += t; //sum加上算出来的2 ^ i } printf("%d\n", sum); return 0; }

#include <stdio.h>int main(){ int input = 0; int result = 1; printf("请输入整数n:“); scanf("%d", &input); if (input < 2) result = 1; else { for (int i=2; i<=input; ++i) result *= i; } printf("%d的阶乘 = %d\n", input, result);}

#include<stdio.h> int fun(int n_); void main() { int n; scanf("%d", &n); int res = fun(n); printf("%d", res); } int fun(int n_) { int res = 1; for(int i = 1; i <= n_; i++) { res = res * i; } return res; }

/*用c语言编写程序 输入一个正整数n,求1到n的数中1出现的个数*/#include"stdio.h" int count(int n) { int i,j,c=0; for(i=1;i<=n;i++) { j=i; while(j!=0) { if(i%10==1) /*判断位数中是否有1*/ { c++; } j=j/10; /*让数除以十让他原来的高位变为低位,即原来的十位变成个位*/ } } return c; } main() { int c,n; printf("input n:\n"); scanf("%d",&n); c=count(n); printf("%d\n",c); }

# includeint main(){int n,i,sum=0;scanf(“%d\n“,&n);for(i=1;i

//因为n不确定,所以最好定义时范围大点,最好是long#include void main() { long n,s,t; s=0; n=1; scanf('%ld',&t); while(n{s=s+n; n=n+1;} printf("1+2+3..+%ld=%ld\n",t,s); }

#include int main () { int n; scanf("%d",&n); if(n { printf("Illegal integer.\n"); return 0; } else printf("%d\n",n==1?1:(1+n)*n/2); //如果n为1,输出1;否则,等差数列算 return 0; }

#include <stdio.h>#include <stdlib.h> int main() { int n, i, r = 1; printf("Input a num:"); scanf("%d", &n); while(n <=0 | n > 10) { printf("Wrong Input!\n"); printf("Input a num:"); scanf("%d", &n); } for(i = 1; i <= n; i++) r = i * r; r = r * (n + 2); printf("result:%d\n", r); return 0; }

#include void main() { int n,nj=1;; printf("输入n:"); scanf("%d",&n); while(n) nj*=n--; printf("n!=%d\n",nj); }

网站首页 | 网站地图
All rights reserved Powered by www.tfsf.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com