資源描述:
《簡(jiǎn)單c語(yǔ)言程序的例子》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在學(xué)術(shù)論文-天天文庫(kù)。
1、例子:l#includemain(){tips();/*caller*/printf(“Successisaboutbangingonafterothershaveletgo”);}tips()/*caller*/{printf(“Whenyoureachtheendoftheropetieaknot&hangon”,)0;}[上述程序的輸出結(jié)果如下:Whenyoureachtheendoftheropttieaknot&hangon.Successisabouthangingonafterother
2、shaveletgo.]l#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比較兩個(gè)數(shù)較大的那個(gè):#includemain(){in
3、ta,b,max;scanf(“%d%d”,&a,&b);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;scan
4、f(“%d%d”,&a,&b);c=max(a,b);printf(“%d”,c);}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請(qǐng)輸入一個(gè)三位數(shù),將其各位逆序輸出:(如153,輸出351)#includevoidmain(){in
5、tx,y,a,b,c;printf("請(qǐng)輸入一個(gè)三位數(shù)的數(shù):");scanf("%d",&x);a=x/100;b=x%100/10;c=x%10;y=c*100+b*10+a;printf("%d",y);}#includevoidmain(){intx,a,b,c,d;printf("請(qǐng)輸入一個(gè)三位數(shù)的數(shù):");scanf("%d",&x);a=x/100;b=x%100/10;c=x%10;d=c*100+b*10+a;printf("%d",d);}買雞:#include
6、voidmain(){intx,y,z;if(x>=0&&x<=19,y>=0&&y<=33,z>=0&&z<=100)while(x=19){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
7、&&x+y+z==100;x=x+1;printf("%d%d%d",x,y,z);}}#includevoidmain(){intx=0,y=0,z=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計(jì)算x
8、=20+3(x-1):#includevoidmain(){intage(intx)inti,j;scanf(“%d”,&i);j=age(i);printf(“age=%d”,j);}intage(intx){intz;if(x==1)z=20;els