資源描述:
《簡單c語言程序的例子》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、例子:l#includemain(){tips();/*caller*/printf(“Successisaboutbangingonafterothershaveletgo”);}tips()/*caller*/{printf(“Whenyoureachtheendoftheropetieaknot&hangon”,)0;}[上述程序的輸出結(jié)果如下:Whenyoureachtheendoftheropttieaknot&hangon.Successisabouthangingonafterothershaveletgo.]l#
2、includemain(){inta,fact;printf(“Enteranynumber”);scanf(“%d”,&a);fact=rec(a);printf(“Factorialvalueis%d”,fact);}rec(x);intx;{intf;if(x==1)return(1);elsef=x*rec(x-1);return(f);}[其顯示的結(jié)果為:Factorialvalueis2.]l比較兩個數(shù)較大的那個:#includemain(){inta,b,max;scanf(“%d%d”,&a,&b)
3、;if(a>b)max=a;elsemax=b;printf(“%d”,max);}l輸出a+b的值:#includevoidmain(){inta,b,x;scanf(“%d%d”,&a,&b);x=a+b;printf(“%d”,x);}l輸出a,b中最大的一位數(shù):#includeintmax(intx,inty){intz;if(x>y)z=x;elsez=y;return(z);}voidmain(){inta,b,c;scanf(“%d%d”,&a,&b);c=max(a,b);printf(“%d”,c
4、);}l輸出Hello:#includeintmain(){printf(“Hello!”);return0;}l求1~100的和:#includeintmain(){ints,n;s=0,n=1;A:s=s+n;n=n+1;if(n<=100)gotoA;printf(“%d”,s);}l請輸入一個三位數(shù),將其各位逆序輸出:(如153,輸出351)#includevoidmain(){intx,y,a,b,c;printf("請輸入一個三位數(shù)的數(shù):");scanf("%d",&x);a=x/100;
5、b=x%100/10;c=x%10;y=c*100+b*10+a;printf("%d",y);}#includevoidmain(){intx,a,b,c,d;printf("請輸入一個三位數(shù)的數(shù):");scanf("%d",&x);a=x/100;b=x%100/10;c=x%10;d=c*100+b*10+a;printf("%d",d);}買雞:#includevoidmain(){intx,y,z;if(x>=0&&x<=19,y>=0&&y<=33,z>=0&&z<=100)while(x=19
6、){x=0;5*x+3*y+z/3==100&&x+y+z==100;x=x+1;}printf("%d%d%d",x,y,z);}y==(100-5*x-z/3)/3&&y==100-x-z;z==(100-5*x-3*y)*3&&z==100-x-y;#includevoidmain(){intx=0,y,z;while(x<=19){5*x+3*y+z/3==100&&x+y+z==100;x=x+1;printf("%d%d%d",x,y,z);}}#includevoidmain(){intx=0,y=0,z
7、=0;while(x<=19){while(y<=33){while(z<=100){z==(100-5*x-3*y)*3&&z==100-x-y;z=z+1;printf("%dt",z);}y==(100-5*x-z/3)/3&&y==100-x-z;y=y+1;printf("%dt",y);}x=x+1;printf("%dt",x);}}l計算x=20+3(x-1):#includevoidmain(){intage(intx)inti,j;scanf(“%d”,&i);j=age(i);printf(“age=%d
8、”,j);}intage(intx){intz;if(x==1)z=20;els