您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业文档 > 简单C语言程序的例子
例子:#includestdio.hmain(){tips();/*caller*/printf(“\nSuccessisaboutbangingonafterothershaveletgo”);}tips()/*caller*/{printf(“\nWhenyoureachtheendoftheropetieaknot&hangon”,)0;}[上述程序的输出结果如下:Whenyoureachtheendoftheropttieaknot&hangon.Successisabouthangingonafterothershaveletgo.]#includestdio.hmain(){inta,fact;printf(“\nEnteranynumber”);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);}[其显示的结果为:Factorialvalueis2.]比较两个数较大的那个:#includestdio.hmain(){inta,b,max;scanf(“%d%d”,&a,&b);if(ab)max=a;elsemax=b;printf(“%d”,max);}输出a+b的值:#includestdio.hvoidmain(){inta,b,x;scanf(“%d%d”,&a,&b);x=a+b;printf(“%d”,x);}输出a,b中最大的一位数:#includestdio.hintmax(intx,inty){intz;if(xy)z=x;elsez=y;return(z);}voidmain(){inta,b,c;scanf(“%d%d”,&a,&b);c=max(a,b);printf(“%d\n”,c);}输出Hello:#includestdio.hintmain(){printf(“Hello!”);return0;}求1~100的和:#includestdio.hintmain(){ints,n;s=0,n=1;A:s=s+n;n=n+1;if(n=100)gotoA;printf(“%d”,s);}请输入一个三位数,将其各位逆序输出:(如153,输出351)#includestdio.hvoidmain(){intx,y,a,b,c;printf(请输入一个三位数的数:);scanf(%d,&x);a=x/100;b=x%100/10;c=x%10;y=c*100+b*10+a;printf(\n%d,y);}#includestdio.hvoidmain(){intx,a,b,c,d;printf(请输入一个三位数的数:);scanf(%d,&x);a=x/100;b=x%100/10;c=x%10;d=c*100+b*10+a;printf(\n%d\n,d);}买鸡:#includestdio.hvoidmain(){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;#includestdio.hvoidmain(){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);}}#includestdio.hvoidmain(){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(%d\t,z);}y==(100-5*x-z/3)/3&&y==100-x-z;y=y+1;printf(%d\t,y);}x=x+1;printf(%d\t,x);}}计算x=20+3(x-1):#includestdio.hvoidmain(){intage(intx)inti,j;scanf(“%d”,&i);j=age(i);printf(“age=%d\n”,j);}intage(intx){intz;if(x==1)z=20;elsez=age(x-1)+3;return(z);}编写程序求圆柱体的表面积和体积:#includestdio.h#definePI3.14/*定义PI为符号常量,值为3.14*/voidmain(){floatr,h,s,v;printf(“请输入半径r和高h的值:\n”);scanf(“%f%f”,&r,&h);/*输入半径r和高h*/s=2*PI*r*h;/*计算圆柱体表面积*/v=PI*r*r*h;/*计算圆柱体体积*/printf(“s=%f,v=%f\n”,s,v);}自增,自减运算符的使用:#includestdio.hvoidmain(){inti=5,j=5;intx,y;x=i++;y=++j;printf(“%d\t%d\n”,-x++,-(++y));printf(“%d\t%d\t%d\t%d\n”,i,j,x,y);}自增、自减运算符的基本运算:#includestdio.hvoidmain(){intk=4;intx,y,z;x=18-k++;printf(“x=%d”,x);printf(“k=%d\n”,k);y=++k+6;printf(“y=%d”,y);printf(“k=%d\n”,k);z=++k+k++;printf(“z=%d\n”,z);}结果:x=14,k=5;y=12,k=6;z=14.输出单个字符:#includestdio.hvoidmain(){chara,b;a=’o’;b=’k’;putchar(a);putchar(b);putchar(‘\n’);}输入字符举例:#includestdio.hvoidmain(){charc;c=getchar();/*从键盘读入一个字符*/putchar(c);/*显示输入的字符*/}猜数游戏:#includestdio.h#includestdlib.h#includetime.hintmain(){intx,i=0,guess;srand(time(NULL));x=rand()%100+1;printf(请你猜数:);do{scanf(%d,&guess);if(xguess){printf(你猜大了!\n);}else{if(xguess)printf(你猜小了!\n);else{printf(你赢了!\n);break;}}i++;if(i=10){printf(随机数是:%d\n,x);printf(你已经猜错了10次,我赢了,哈哈!\n);break;}printf(请你再猜:);}while(1);}#includestdio.hvoidmain(){inta=2,*p=&a,*q=&a;Printf(%d,%d\n,*p++,*(q++));P=&a;q=&;Printf(%d%d\n,*p,(*q)++);Printf(%d%d\n,*p,++(*q);}结果为:2,22,24,4(从又向左算)#includestdio,hVoidmain(){Inta,b,c;Int*pa,*pb,*pc;Pa=&a;pb=&b;pc=&c;Scanf(%d%d,pa,pb);Printf(a=%d,b=%d\n,*pa,*pb);C=a+b;Printf(c=%d\n,*pc);*pc=a+*pb;Printf(c=%d从100到200之间有哪些数除4多2,除7多3,除9多5?#includestdio.hintmain(){for(inti=100;i=200;i++)if(i%4==2&&i%7==3&&i%9==5)printf(%d\n,i);return0;}用选择法对10个整数排序:#includestdio.hVoidmain(){Int*p,i,a[10];P=a;For(i=0;i10;i++)Scanf(
本文标题:简单C语言程序的例子
链接地址:https://www.777doc.com/doc-6898752 .html