收藏 分销(赏)

简单C语言程序的例子.doc

上传人:pc****0 文档编号:7925785 上传时间:2025-01-27 格式:DOC 页数:9 大小:58.50KB 下载积分:10 金币
下载 相关 举报
简单C语言程序的例子.doc_第1页
第1页 / 共9页
简单C语言程序的例子.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
例子: l #include <stdio.h> main () { tips(); /*caller*/ printf(“\n Success is about banging on after others have let go”); } tips() /*caller*/ { printf (“\n When you reach the end of the rope tie a knot & hang on”,)0; } [上述程序的输出结果如下: When you reach the end of the ropt tie a knot & hang on. Success is about hanging on after others have let go.] l #include <stdio.h> main() { int a,fact; printf(“\nEnter any number”); scanf(“%d”,&a); fact=rec(a); printf(“Factorial value is%d”,fact); } rec(x); int x; { int f; if(x==1) return(1); else f=x*rec(x-1); return(f); } [其显示的结果为: Factorial value is 2.] l 比较两个数较大的那个: #include <stdio.h> main() { int a,b,max; scanf (“%d%d”,&a,&b); if (a>b) max=a; else max=b; printf (“%d”,max); } l 输出a+b的值: #include <stdio.h> void main() { int a,b,x; scanf (“%d%d”,&a,&b); x=a+b; printf (“%d”,x); } l 输出a,b中最大的一位数: #include <stdio.h> int max(int x,int y) { int z; if (x>y) z=x; else z=y; return(z); } void main() { int a,b,c; scanf (“%d%d”,&a,&b); c=max(a,b); printf (“%d\n”,c); } l 输出Hello: #include <stdio.h> int main() { printf (“Hello!”); return 0; } l 求1~100的和: #include <stdio.h> int main() { int s,n; s=0,n=1; A: s=s+n; n=n+1; if (n<=100) goto A; printf (“%d”,s);} l 请输入一个三位数,将其各位逆序输出:(如153,输出351) #include <stdio.h> void main() { int x,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); } #include <stdio.h> void main() { int x,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); } 买鸡: #include <stdio.h> void main() { int x,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; #include <stdio.h> void main() { int x=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); } } #include <stdio.h> void main() { int x=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); } } l 计算x=20+3(x-1): #include <stdio.h> void main() { int age(int x) int i,j; scanf(“%d”,&i); j=age(i); printf(“age=%d\n”,j); } int age(int x) {int z; if(x==1) z=20; else z=age(x-1)+3; return(z); } l 编写程序求圆柱体的表面积和体积: #include <stdio.h> #define PI 3.14 /*定义PI为符号常量,值为3.14*/ void main() { float r,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); } l 自增,自减运算符的使用: #include <stdio.h> void main() { int i=5,j=5; int x,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); } l 自增、自减运算符的基本运算: #include <stdio.h> void main() { int k=4; int x,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. l 输出单个字符: #include <stdio.h> void main() { char a,b; a=’o’; b=’k’; putchar(a); putchar(b); putchar(‘\n’); } l 输入字符举例: #include <stdio.h> void main() { char c; c=getchar(); /*从键盘读入一个字符*/ putchar(c); /*显示输入的字符*/ } 猜数游戏: #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int x,i=0,guess; srand(time(NULL)); x=rand()%100+1; printf("请你猜数:"); do { scanf("%d",&guess); if(x<guess) { printf ("你猜大了!\n"); } else { if(x>guess) printf("你猜小了!\n"); else { printf("你赢了!\n"); break; } } i++; if (i>=10) { printf("随机数是:%d\n",x); printf("你已经猜 错了10次,我赢了,哈哈!\n"); break; } printf("请你再猜:"); }while(1); } #include <stdio.h> void main() { int a=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,2 2,2 4,4 (从又向左算) #include <stdio,h> Void main() { Int a,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? #include<stdio.h> int main() { for(int i = 100; i <= 200 ; i++) if(i%4==2&&i%7==3&&i%9==5) printf("%d\n",i); return 0; } 用选择法对10个整数排序: #include <stdio.h> Void main() { Int *p,i,a[10]; P=a; For (i=0;i<10;i++) Scanf (
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服