资源描述:
《c语言小程序练习.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、从键盘上输入两个整数,求出它们的和#includevoidmain(){intx,y,s;scanf("%d,%d,%d",&x,&y,&s);if(s==x+y)是==不是=printf("yes");elseprintf("wrong");}已知三角形的底和高,求出三角形的面积。#includevoidmain(){intx,y;floats;scanf("%d,%d",&x,&y);s=x*y/2;不能写成1/2*x*yprintf("%f",s);}输入3,5输出7.00000
2、0#includevoidmain(){floats,x,y;scanf("%f,%f",&x,&y);s=x*y/2;printf("%f",s);}输入3,5输出7.500000已知三角形的三边长,求出三角形的面积。#include#includevoidmain(){floata,b,c,p,s;scanf("%f,%f,%f",&a,&b,&c);p=(a+b+c)/2;s=sqrt(p*(p-a)*(p-b)*(p-c));printf("%f",s);}
3、已知二元一次方程的三个系数,求方程的一个根。.编程实现符号函数。当x<0,则sgn(x)=-1,当x>0,则sgn(x)=+1,当x=0,则sgn(x)=0#includevoidmain(){intx,y;scanf("%d",&x);不能直接定义sgn(x)出错'=':leftoperandmustbel-valueif(x<0)y=-1;if(x==0)y=0;elsey=1;printf("sgn(%d)=%d",x,y);}输入5输出sgn(5)=1从键盘上输入数字星期,在屏幕上显示英文星
4、期。#includevoidmain(){intx;scanf("%d",&x);switch(x){case0:printf("Sunday");break;case1:printf("Monday");break;如果没有break;输入5输出Fridaycase2:printf("Tuesday");break;Saturdaycase3:printf("Wendesday");break;Don'thavethisdaycase4:printf("Tursday");brea
5、k;case5printf("Friday");break;case5printf("Saturday");break;default:printf("Don'thavethisday");}从键盘输入三角形的三边长,判断出这三边能否构成三角形#includevoidmain(){inta,b,c;scanf("%d,%d,%d",&a,&b,&c);if((a+b>c)&&(b+c>a)&&(a+c>b))分号一定是英文分号否则unknowncharacter'0xa3'printf("y
6、es");elseprintf("no");}从键盘输入三个整数,将这三个数按从大到小的顺序排列起来#includevoidmain(){inta,b,c,temp;scanf("%d,%d,%d",&a,&b,&c);if(a>b)temp=a;a=b;b=temp;if(b>c)b=temp;b=c;c=temp;printf("%d,%d,%d",a,b,c);}#includevoidmain(){floatx;inty;scanf("%f",&x);(y=(x>0)?1):(
7、y=(x<0)?-1):0;printf("%d",y);}使用for循环编程求出1到100的和#includevoidmain(){inti,j,k;j=0;printf("求前k项和:");scanf("%d",&k);for(i=1;i<=k;i++)j=j+i;printf("%d",j);}使用当循环编程求出1到100的和#includevoidmain(){inti,j,k;j=0;i=1;一定要给付i一个初值or输入100没有往下运行printf("求前k项和:");
8、scanf("%d",&k);while(i<=k){j=j+i;i++;}printf("%d",j);}从键盘输入10个实数,求出最大值#includevoidmain(){inti,k;floatx,max;scanf("%d",&k);输入k个数求最大数;scanf("%f",&x);注