资源描述:
《C语言经典例题.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、赞赞赞1.输入两个正整数,m和n,求其最大公约数和最小公倍数。#includevoidmain(){inthcf(int,int);/*函数声明*/intlcd(int,int,int);/*函数声明*/intu,v,h,l;printf("Pleaseinputtwonumbers:");scanf("%d,%d",&u,&v);h=hcf(u,v);printf("H.C.F=%d",h);l=lcd(u,v,h);printf("L.C.D=%d",l);}inthcf(intu,intv){in
2、tt,r;if(v>u){t=u;u=v;v=t;}while((r=u%v)!=0){u=v;v=r;}return(v);}intlcd(intu,intv,inth){return(u*v/h);}2.输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。#includeintletter,digit,space,others;voidmain(){voidcount(char[]);chartext[80];printf("Pleaseinputstring:");gets(text);prin
3、tf("string:");puts(text);letter=0;digit=0;space=0;others=0;count(text);printf("letter:%d,digit:%d,space:%d,others:%d",letter,digit,space,others);}voidcount(charstr[]){inti;for(i=0;str[i]!=' ';i++)if((str[i]>='a'&&str[i]<='z')
4、
5、(str[i]>='A'&&str[i]<='Z'))letter++;e
6、lseif(str[i]>='0'&&str[i]<='9')digit++;elseif(str[i]==32)space++;elseothers++;}3.输入一个正整数求出它是几位数;输出原数和位数。#includeintdigit;voidmain(){voidcount(char[]);chartext[80];printf("Pleaseinputnumbers:");gets(text);printf("Numbers:");puts(text);digit=0;count(text);pr
7、intf("digit:%d",digit);}voidcount(charstr[]){inti;for(i=0;str[i]!=' ';i++)if(str[i]>='0'&&str[i]<='9')digit++;}4.输入一个正整数,输出原数并逆序打印出各位数字。#includevoidinvertLongInt(long);voidmain(){unsignedlongiNumber;printf("Pleaseinputanumber:");scanf("%ld",&iNumber);prin
8、tf("Theinputnumberis:%ld",iNumber);printf("Theinversenumberis:");invertLongInt(iNumber);}voidinvertLongInt(longx){if(x>=0&&x<=9)printf("%d",x);else{printf("%d",x%10);invertLongInt(x/10);}}5.从键盘上输入若干学生的一门课成绩,统计并输出最高成绩和最低成绩及相应的序号,当输入负数时结束输入。6.从键盘上输入若干学生的一门课成绩,计算出平均分,
9、当输入负数时结束输入。将结果输出。7.求1!+2!+3!+……+20!,将结果输出。#includevoidmain(){floats=0,t=1;intn;for(n=1;n<=20;n++){t=t*n;s=s+t;}printf("1!+2!+3!+……+20!=%e",s);}8.打印以下图案:****************#includevoidmain(){inti,j;printf("Thepictureis:");staticcharpicture[4][7]={{'',
10、'','','*'},{'','','*','*','*'},{'','*','*','*','*','*'},{'*','*','*','*','*','*','*'}};for(i=0;i<=3;i++){for(j=0;j<=6