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。
總結
以上是生活随笔為你收集整理的c语言初学者程序,C语言初学者必懂的100个范例程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 斐讯k2刷无线打印服务器,斐讯K2无线路
- 下一篇: 常用键盘ASCⅡ码,方便查阅