资源描述:
《几个实用的C语言代码供初学者.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、阶乘函数#includeintsum(inty);intjiechen(intx);voidmain(){inta,b;printf("shuruyigeshu:");scanf("%d",&a);b=sum(a);printf("%d",b);}intjiechen(intx){ints=1,n;for(n=x;n>0;n--)s*=n;returns;}intsum(inty){intm=0,n;for(n=y;n>0;n--)m+=jiechen(n);returnm;}输入注意#includevoidmain(){inta;sc
2、anf("a=%d",&a);printf("a=%d",a);}#includeintd;//系统默认为0;在函数外voidmain(){inta=1,b=0,c=1;if(a&&b&&(c=c+1)==0)//逻辑表达式短路d++;printf("%d%d%d%d",a,b,c,d);}输出*阵#include#defineSPACE32voidstar(inti,intn);voidmain(){inti,n=10;for(i=1;i<=n;i++)star(i,n);}voidstar(inti,intn){intj;for(j=1
3、;j<=n-i;j++)printf("%c",SPACE);for(j=1;j<=2*i-1;j++)printf("%c",'*');printf("");}/*求解a*x=b*/#includevoidmain(){inta,b,x;printf("输入a,b:");scanf("%d%d",&a,&b);if(a==0&&b!=0)printf("Novalue");if(a==0&&b==0)printf("x=Everyvalue");if(a&&b)printf("x=%f",(float)b/a);}/*九九表*/#include4、io.h>voidmain(){inti,j;printf("%6c",32);for(i=1;i<10;i++)printf("%4d",i);printf("");for(i=1;i<10;i++){printf("%4d%2c",i,32);for(j=1;j<10;j++){if(j==9)printf("%4d",i*j);elseprintf("%4d",i*j);}}}/*求素数*/#include#includeintprime(intx);voidmain(){inti;for(i=2;i<100;i++)if(
5、prime(i))printf("%3d",i);printf("");}intprime(intx){intm;m=sqrt(x);if(m==1)return1;else{inti;for(i=2;i<=m;i++)if(x%i==0)return0;return1;}}/*求最大值*/#includevoidmain(){inti,j,max,a[]={25,64,38,40,75,66,38,54};max=a[0];for(i=1;i<8;i++)if(a[i]>max){max=a[i];j=i+1;}printf("%2d%2d",ma
6、x,j);}/*删除*/#includeintdel(inta[],intn,intb[])//删除函数{inti,k=1;b[0]=a[0];for(i=1;i