c语言初学者程序,C语言初学者必懂的100个范例程序

《C语言初学者必懂的100个范例程序》由会员分享,可在线阅读,更多相关《C语言初学者必懂的100个范例程序(72页珍藏版)》请在人人文库网上搜索。

1、经典C语言程序第一例【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。 2.程序源代码:main()int i,j,k;printf(n);for(i=1;i2)/*如果是闰年且月份大于2,总天数应该加一天*/sum+;printf(It is the %dth day.,sum);经典C语言程序第五例【程序5】题目:输入三个整数x,y,z,请把这三个数由小到大输出。1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果xy则将x与y的值。

2、进行交换,然后再用x与z进行比较,如果xz则将x与z的值进行交换,这样能使x最小。2.程序源代码:main()int x,y,z,t;scanf(%d%d%d,&x,&y,&z);if (xy)t=x;x=y;y=t; /*交换x,y的值*/if(xz)t=z;z=x;x=t;/*交换x,z的值*/if(yz)t=y;y=z;z=t;/*交换z,y的值*/printf(small to big: %d %d %dn,x,y,z);经典C语言程序第六例【程序6】题目:用*号输出字母C的图案。1.程序分析:可先用*号在纸上写出字母C,再分行输出。2.程序源代码:#include stdio.hma。

3、in()printf(Hello C-world!n);printf( *n);printf( *n);printf( * n);printf( *n);经典C语言程序第七例【程序7】题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!1.程序分析:字符共有256个。不同字符,图形不一样。2.程序源代码:#include stdio.hmain()char a=176,b=219;printf(%c%c%c%c%cn,b,a,a,a,b);printf(%c%c%c%c%cn,a,b,a,b,a);printf(%c%c%c%c%cn,a,a,b,a,a);printf。

4、(%c%c%c%c%cn,a,b,a,b,a);printf(%c%c%c%c%cn,b,a,a,a,b);经典C语言程序第八例【程序8】题目:输出9*9口诀。1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。2.程序源代码:#include stdio.hmain()int i,j,result;printf(n);for (i=1;ik,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。2.程序源代码:/* zheng int is divided yinshu*/main()i。

5、nt n,i;printf(nplease input a number:n);scanf(%d,&n);printf(%d=,n);for(i=2;i=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。1.程序分析:(ab)?a:b这是条件运算符的基本例子。2.程序源代码:main()int score;char grade;printf(please input a scoren);scanf(%d,&score);grade=score=90?A:(score=60?B:C);printf(%d belongs to %c,score,grade);经典C语言程序第。

6、十六例【程序16】题目:输入两个正整数m和n,求其最大公约数和最小公倍数。1.程序分析:利用辗除法。2.程序源代码:main()int a,b,num1,num2,temp;printf(please input two numbers:n);scanf(%d,%d,&num1,&num2);if(num1temp=num1;num1=num2;num2=temp;a=num1;b=num2;while(b!=0)/*利用辗除法,直到b为0为止*/temp=a%b;a=b;b=temp;printf(gongyueshu:%dn,a);printf(gongbeishu:%dn,num1*nu。

7、m2/a);经典C语言程序第十七例【程序17】题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。1.程序分析:利用while语句,条件为输入的字符不为n.2.程序源代码:#include stdio.hmain()char c;int letters=0,space=0,digit=0,others=0;printf(please input some charactersn);while(c=getchar()!=n)if(c=a&c=A&c=0&c0)x1=(x2+1)*2;/*第一天的桃子数是第2天桃子数加1后的2倍*/x2=x1;day-;printf(the t。

8、otal is %dn,x1);经典C语言程序第二十二例【程序22】题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。2.程序源代码:main()char i,j,k;/*i是a的对手,j是b的对手,k是c的对手*/for(i=x;ivoid main()char letter;printf(please input the first 。

9、letter of somedayn);while (letter=getch()!=Y)/*当所按字母为Y时才结束*/ switch (letter)case S:printf(please input second lettern);if(letter=getch()=a)printf(saturdayn);else if (letter=getch()=u)printf(sundayn);else printf(data errorn);break;case F:printf(fridayn);break;case M:printf(mondayn);break;case T:print。

10、f(please input second lettern);if(letter=getch()=u)printf(tuesdayn);else if (letter=getch()=h)printf(thursdayn);else printf(data errorn);break;case W:printf(wednesdayn);break;default: printf(data errorn);经典C语言程序第三十二例【程序32】题目:Press any key to change color, do you want to try it. Please hurry up!1.程序分。

11、析:2.程序源代码:#include void main(void)int color;for (color = 0; color void main(void)clrscr();/*清屏函数*/textbackground(2);gotoxy(1, 5);/*定位函数*/cprintf(Output at row 5 column 1n);textbackground(3);gotoxy(20, 10);cprintf(Output at row 10 column 20n);经典C语言程序第三十四例【程序34】题目:练习函数调用1. 程序分析: 2.程序源代码:#include void 。

12、hello_world(void)printf(Hello, world!n);void three_hellos(void)int counter;for (counter = 1; counter void main(void)int color;for (color = 1; color #include math.h#define N 101main()int i,j,line,aN;for(i=2;iaj) min=j;tem=ai;ai=amin;amin=tem;/*output data*/printf(After sorted n);for(i=0;iend)a10=numb。

13、er;elsefor(i=0;inumber)temp1=ai;ai=number;for(j=i+1;j);scanf(%d,&num);printf(40:The square for this number is %d n,SQ(num);if(num=50)again=TRUE;elseagain=FALSE;经典C语言程序第四十七例【程序47】题目:宏#define命令练习(2)1.程序分析:2.程序源代码:#include stdio.h#define exchange(a,b) /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上*/int t;t=a;a=b;b=t;v。

14、oid main(void)int x=10;int y=20;printf(x=%d; y=%dn,x,y);exchange(x,y);printf(x=%d; y=%dn,x,y);经典C语言程序第四十八例【程序48】题目:宏#define命令练习(3)1.程序分析:2.程序源代码:#define LAG #define SMA y)?x:y#define MINIMUM(x,y) (xy)?y:xvoid main() int a=10,b=20;#ifdef MAXprintf(40: The larger one is %dn,MAXIMUM(a,b);#elseprintf(40。

15、: The lower one is %dn,MINIMUM(a,b);#endif#ifndef MINprintf(40: The lower one is %dn,MINIMUM(a,b);#elseprintf(40: The larger one is %dn,MAXIMUM(a,b);#endif#undef MAX#ifdef MAXprintf(40: The larger one is %dn,MAXIMUM(a,b);#elseprintf(40: The lower one is %dn,MINIMUM(a,b);#endif#define MIN#ifndef MINp。

16、rintf(40: The lower one is %dn,MINIMUM(a,b);#elseprintf(40: The larger one is %dn,MAXIMUM(a,b);#endif经典C语言程序第五十例【程序50】题目:#include 的应用练习1.程序分析:2.程序源代码:test.h 文件如下:#define LAG #define SMA #define EQ =#include test.h /*一个新文件50.c,包含test.h*/#include stdio.hvoid main() int i=10;int j=20;if(i LAG j)printf(。

17、40: %d larger than %d n,i,j);else if(i EQ j)printf(40: %d equal to %d n,i,j);else if(i SMA j)printf(40:%d smaller than %d n,i,j);elseprintf(40: No such value.n);经典C语言程序第五十一例【程序51】题目:学习使用按位与 & 。1.程序分析:0&0=0; 0&1=0; 1&0=0; 1&1=12.程序源代码:#include stdio.hmain()int a,b;a=077;b=a&3;printf(40: The a & b(decimal) is %d n,b);b&=7;printf(40: The a & b(decimal) is %d n,b);经典C语言程序第五十二例【程序52】题目:学习使用按位或 | 。1.程序分析:0|0=0; 0|1=1; 1|0=1; 1|1=12.程序源代码:#include stdio.hmain()int a,b;a=077;b=a|3;printf(40: The a & b(decimal) is %d n,b);b|=7;printf(40: The a & b(decimal) is %d n,b);经典C语言程序第五十三例【程序53】题目:学习使用按位异或 。1。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值