您好,欢迎访问三七文档
1.要求在屏幕上输出下一行信息。Thisisacprogram.程序:#includestdio.hintmain(){printf(“thisisacprogram.\n”);return0;}2.求两个整数之和。程序:#includestdio.hintmain(){inta,b,sum;a=122;b=234;sum=a+b;printf(“sumis%d\n”,sum);return0;}3.求两个整数之间的较大者。程序:#includestdio.hintmain(){intmax(intx,inty);inta,b,c;scanf(%d,%d,&a,&b);c=max(a,b);printf(max=%d\n,c);return0;}intmax(intx,inty){intz;if(xy)z=x;elsez=y;return(z);}4.有人用温度计测量出华氏发表示的温度(如69°F),今要求把她转换成以摄氏法表示的温度(如20℃)。公式:c=5(f-32)/9.其中f代表华氏温度,c代表摄氏温度。程序:#includestdio.hintmain(){floatf,c;f=64.0;c=(5.0/9)*(f-32);printf(f=%f\nc=%f\n,f,c);return0;}5.计算存款利息。有1000元,想存一年。有一下三种方法可选:(1)活期:年利率为r1;(2)一年定期:年利率为r2;(3)存两次半年定期:年利率为r3。分别计算一年后按三种方法所得到的本息和。程序:#includestdio.hintmain(){floatp0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;p1=p0*(1+r1);p2=p0*(1+r2);p3=p0*(1+r3/2)*(1+r3/2);printf(p1=%f\np2=%f\np3=%f\n,p1,p2,p3);return0;}6.给定一个大写字母,要求以小写字母输出。程序:#includestdio.hintmain(){charc1,c2;c1=’A’;c2=c1+32;printf(“%c\n”,c2);printf(“%d\n”,c2);return0;}7.给出三角形的三边长,求三角形的面积。公式:若给定三角形的三边长,且任意两边之长大于第三边。则:area=√s(s−a)(s−b)(s−c)其中s=(a+b+C)/2.程序:#includestdio.h#includemath.hintmain(){doublea,b,c,area;a=3.67;b=5.43;c=6.21;s=(a+b+c)/2;area=sqrt(s*(s-a)*(s-b)*(s-c));printf(“a=%f\tb=%f\tc=%f\n”,a,b,c);printf(“area=%f\n”,area);return0;}8.求ax2+bx+c=0方程的根。a,b,c由键盘输入,设b2-4ac0.程序:#includestdio.h#includemath.hintmain(){doublea,b,c,disc,x1,x2,p,q;scanf(“%lf%lf%lf”,&a,&b,&c);disc=b*b-4*a*c;if(disc0)printf(“Thisquestionhasnorealroots\n”);else{p=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf(“x1=%7.2f\nx2=%7.2f\n”,x1,x2);}return0;}9.先后输出BOY三个字符。程序:#includestdio.hintmain(){chara=’B’,b=’O’,c=’Y’;putchar(a);putchar(b);putchar(c);putchar(‘\n’);return0;}10.用三个getchar函数先后向计算机输入BOY三个字符,然后用putchar函数输出。程序:#includestdio.hintmain(){chara,b,c;a=getchar();b=getchar();c=getchar();putchar(a);putchar(b);putchar(c);putchar(‘\n’);return0;}或#includestdio.hintmain(){putchar(getchar());putchar(getchar());putchar(getchar());putchar(‘\n’);return0;}11.用getchar函数从键盘读入一个大写字母,把它转换成小写字母,然后用getchar函数输出对应的小写字母。程序:#includestdio.hintmain(){charc1,c2;c1=getchar();c2=c1+32;putchar(c2);putchar(‘\n’);return0;}12.输入两个实数,按代数值由小到大的顺序输出这两个数。(参照将两个杯子中的水互换,必须借助第三个杯子)。程序:#includestdio.hintmain(){floata,b,t;scanf(“%f,%f”,&a,&b);if(ab){t=a;a=b;b=t;}printf(“%5.2f,%5.2f\n”,a,b);return0;}13.输入a,b,c三个数,要求由小到大的顺序输出。程序:#includestdio.hintmain(){floata,b,c,t;scanf(%f,%f,%f,&a,&b,&c);if(ab);{t=a;a=b;b=t;}if(ac){t=a;a=c;c=t;}if(bc){t=b;b=c;c=t;}printf(%5.2f,%5.2f,%5.2f\n,a,b,c);return0;}14.输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母,如果不是,不转换。然后输出最后得到的字符。程序:#includestdio.hintmain(){charch;scanf(“%c”,&ch);ch=(ch=’A’&&ch=’Z’)?(ch+32):ch;printf(“%c\n”,ch);return0;}或#includestdio.hintmain(){charch;scanf(%c,&ch);if(ch='A'&&ch='Z')printf(%c\n,ch+32);elseprintf(%c\n,ch);return0;}15.有一个函数:y={−1(x0)0(x=0)1(x0)编一程序。输入一个x的值,要求输出相应的y值。程序:#includestdio.hintmain(){intx,y;scanf(%d,&x);if(x0)y=-1;elseif(x==0)y=0;elsey=1;printf(x=%d\ny=%d\n,x,y);return0;}16.要求按照考试成绩的等级输出百分制分数段,A等为85分以上,B等为70-84分。C等为60-69分,D等为60分以下。成绩的等级由键盘输入。程序:#includestdio.hintmain(){chargrade;scanf(%c,grade);printf(Youscore:\n);switch(grade){case'A':printf(85~100\n);break;case'B':printf(70~84\n);break;case'C':printf(60~69\n);break;case'D':printf(60\n);break;default:printf(enterdateerror\n);}return0;}17.写一程序,判断某一年是否为闰年。程序:#includestdio.hintmain(){intleap,year;printf(pleaseenteryear:);scanf(%d,&year);if(year%4==0){if(year%100==0){if(year%400==0)leap=1;elseleap=0;}elseleap=1;}elseleap=0;if(leap)printf(%disaleapyear\n,year);elseprintf(%disnotaleapyear\n,year);return0;}或#includestdio.hintmain(){intleap,year;printf(pleaseenteryear:);scanf(%d,&year);if(year%4!=0)leap=0;elseif(year%100!=0)leap=1;elseif(year%400!=0)leap=0;elseleap=1;if(leap==1)printf(%disaleapyear\n,year);elseprintf(%disnotaleapyear\n,year);return0;}或#includestdio.hintmain(){intleap,year;printf(pleaseenteryear:);scanf(%d,&year);if((year%4==0&&year%100!=0)||(year%400==0))leap=1;elseleap=0;if(leap==1)printf(%disaleapyear\n,year);elseprintf(%disnotaleapyear\n,year);return0;}18.求ax2+bx+c=0方程的根。a,b,c由键盘输入。(完整版)程序:#includestdio.h#includemath.hintmain(){doublea,b,c,disc,x1,x2,x3,realpart,imagepart;scanf(%lf,%lf,%lf,&a,&b,&c);printf(Theequation);if(fabs(a)=1e-6)printf(isnotaquadratic);else{disc=b*b-4*a*c;if(fabs(disc)=1e-6)printf(hastwoequalroots%8.4f\n,-b/2*a);elseif(disc1e-6){printf(hastwodistinctrealroots\n%8.4f,%8.4f,x1,x2);x1=(-b+sqrt(disc))/2*a;x2=(-b-sqrt(disc))/2*a;}else{realpart=-b/2*a;imagepart=sqrt(-disc)/2*a;printf(hastwocomplexroots:\n);printf(%8.4f+%8.4fi\n,realpart,imagepart);printf(%8.4f-%8.4fi\n,realpart,imagepart);}}return0;}//注释:由于b*b-4*a*c(disc)是实数,而实数在计算和存储时会有一些微小的误差,因此不能直接进行如下判断://“if(disc==0),因为这样可能出现本来是零的量,由于上述误差而判别为不等于零而导致结果错误,//所以采取的办法是判别disc的绝对值(fabs(disc))是否小于一个很小的数(例如:1e-6)。如果小于此数,则认为disc=0.19.给出一个不多出5位的正整数,要求:(1):求出它是几位数;(2):分别输出每一位数字;(3):按逆序输出各位数字,例如原数为321,输出123。程序:#includestdio.hintmain(){intnum,indiv,ten,hundred,thousand,ten_thousand,place;printf(请输入一个正整数(0~99999):);scanf(%d,&num);if(num9999)place
本文标题:c语言程序代码
链接地址:https://www.777doc.com/doc-5251863 .html