資源描述:
《c語言平時練習(xí)小例子.doc》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、給一個不多于4位的正整數(shù),求出它是幾位數(shù),逆序打印出各位數(shù)字#includevoidmain(){intx,a,b,c,d;scanf("%d",&x);a=x/1000;b=x%1000/100;c=x%100/10;d=x%10;if(a!=0)printf("4:%d%d%d%d",d,c,b,a);elseif(b!=0)printf("3:%d%d%d",d,c,b);elseif(c!=0)printf("2:%d%d",d,c);elseif(d!=0)printf("1:%d",d);}從10至20之間的素數(shù),自動
2、輸出(我的)#include#includevoidmain(){intx,k;for(x=10;x>=10&&x<=20;x++){for(k=2;k<=sqrt(x);k++)if(x%k==0)break;if(k>sqrt(x))printf("%d",x);}}0-9這十個數(shù)字組成多少個無重復(fù)的三位數(shù)#includevoidmain(){intx,a,b,c,num=0;for(x=100;x<=999;x++){a=x/100;b=x/10%10;c=x%10;if(a!=b&&a!=c&&b!=c)
3、{num++;printf("%5d",x);}}printf("number=%d",num);}求300內(nèi)能被17整除的最大數(shù)#includevoidmain(){intx;for(x=300;x>=1;x--)if(x%17==0)break;printf("x=%d",x);}斐波那契數(shù)列#includevoidmain(){longf1=1,f2=1,f3;intk;printf("%1dt%1dt",f1,f2);for(k=3;k<=30;k++){f3=f1+f2;printf("%dt",f3);
4、f1=f2;f2=f3;}}求300以內(nèi)能被17整除的所有整數(shù),用continue#includevoidmain(){intx;for(x=1;x<=300;x++){if(x%17!=0)continue;printf("%dt",x);}}輸出三角形星花#includevoidmain(){intk1,k2;for(k1=1;k1<=4;k1++){putchar('t');for(k2=1;k2<=k1;k2++)putchar('b');for(k2=1;k2<=k1*2-1;k2++)putchar('*');
5、putchar('');}}求s=2/1-3/2+5/3-8/5…求前十項的和#includevoidmain(){floats=0,f1=2,f2=1,f=1,t,n;for(n=1;n<=10;n++){s=s+f*f1/f2;f=f*(-1);t=f2;f2=f1;f1=f1+t;}printf("s=%f",s);}逆序輸出任意整數(shù)#includevoidmain(){longx,y;scanf("%d",&y);while(y!=0){x=y%10;printf("%d",x);y=y/10;}}函數(shù)求和#in
6、cludevoidmain(){floatadd(float,float);floata,b,c;scanf("%f,%f",&a,&b);c=add(a,b);printf("sumis%f",c);}floatadd(floatx,floaty){floatz;z=x+y;return(z);}輸入年月日輸出該天在該年的天數(shù)#includeintleap(intyear){intlp;lp=(year%4==0&&year%100!=0
7、
8、year%400==0)?1:0;returnlp;}intmonth_day(int
9、year,intmonth){intd;switch(month){case1:case3:case5:case7:case8:case10:case12:d=31;break;case2:d=leap(year)?29:28;break;default:d=30;}returnd;}intdays(intyear,intmonth,intday){inti,ds=0;for(i=1;i10、scanf