《C语言基础编程练习》由会员分享,可在线阅读,更多相关《C语言基础编程练习(94页珍藏版)》请在金锄头文库上搜索。
1、精选优质文档-倾情为你奉上1.屏幕上输入:This is a C program#include int main()printf(This is a C program.n);return 0;2、求两个整数之和#include void main()int a,b,sum;a=123;b=456;sum=a+b;printf(%dn,sum);3、求两个整数中的较大者#include void main()int max(int x,int y);int a,b,c;scanf(%d,%d,&a,&b);c=max(a,b);printf(max=%d,c);int max(int x,i
2、nt y)int z;if(xy)z=x;elsez=y;return(z);4、编写一个程序,输入a,b,c三个值,输出其中最大者#include void main()int a,b,c,max; printf(input a,b,c:n); scanf(%d,%d,%d,&a,&b,&c); max=a; if(maxb) max=b; if(maxc) max=c; printf(最大数是%dn,max);5、求5!#include void main()int t=1,i=2; while(i=5) t=t*i; i=i+1; printf(%dn,t);6、求多项式1-1/2+1/
3、3-1/4+1/99-1/100的值#include void main() int sign=1; double deno=2.0,sum=1.0,term; while(deno=100) sign=-sign; term=sign/deno; sum=sum+term; deno=deno+1; printf(%fn,sum);7、将华氏温度64F转换为摄氏度#include void main() float f,c; f=64.0; c=(5.0/9.0)*(f-32); printf(c=%fn,c);8、1000元存一年:(1)活期,年利率为r1;(2)一年期顶起,年利率r2;(
4、3)存两次半年定期,年利率为r3.分别计算3种方法本息和#include void main() float p0=1000,r1=0.0036,r2=0.0225,r3=0.0193,p1,p2,p3; p1=p0*(1+r1); p2=p0*(1+r2); p3=p0*(1+r3/2)*(1+r3/2); printf(p1=%fnp2=%fnp3=%fn,p1,p2,p3);9、给定一个大写字母A,输出其小写字母#include void main()char c1,c2;c1=A;c2=c1+32;printf(%cn,c2);printf(%dn,c2);10、给出三角形三边长,求面
5、积#include #include int main()double a,b,c,s,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=%ftb=%ftc=%fn,a,b,c);printf(area=%fn,area);11、输入一个double类型的数,使该数保留小数点后两位,对第三位小数进行四舍五入后处理,然后输出此数,以便验证处理是否正确。#includevoid main()double x;printf(Enter x:);scanf(%lf,&x);printf(1)x=%f
6、n,x);x=x*100+0.5;x=(int)x;x=x/100;printf(2)x=%fn,x);12、编程从键盘输入两个整数给变量x和y,然后输出x和y的值,最后交换x和y的值并输出。#includevoid main()int x,y,t;printf(Enter x,y:n);scanf(%d %d,&x,&y);printf(x=%d y=%dn,x,y);t=x;x=y;y=t;printf(x=%d y=%dn,x,y);13、从键盘输入两个两位的正整数给变量x和y,并将x和y合并形成一个整数放在变量z中。合并的方式是:将数x的十位和各位依次放在z的千位和十位,将y的十位和个
7、位放在z的个位和百位上。#includevoid main()int x,y,z;printf(Input x,y:);scanf(%d,%d,&x,&y);x=x%100;y=y%100;z=(x/10)*1000+(x%10)*10+y/10+(y%10)*100;printf(x=%d,y=%d,z=%dn,x,y,z);14、输入3个整数,分别放入在变量a,b,c中,程序把输入的数据重新按由从小到大的顺序放在变量a,b,c中,最后输出a,b,c中的值#includevoid main()int a,b,c,t;printf(input a,b,c:);scanf(%d%d%d,&a,&
8、b,&c);printf(a=%d,b=%d,c=%dn,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(a=%d,b=%d,c=%dn,a,b,c);15、根据输入的学生成绩,给出相应的等级,90分以上的等级为A,60以下为E,其余每10分一个等级。#includevoid main()int g;printf(Enter g:);scanf(%d,&g);printf(g=%d:,g);if(g=90) printf(An);else if(g=80) printf(Bn);else if(g=70)
9、printf(Cn);else if(g=60) printf(Dn);else printf(En);#includevoid main()int g;printf(Enter g:);scanf(%d,&g);printf(g=%d:,g);switch(g/10)case 9: printf(An);break;case 8: printf(Bn);break;case 7: printf(Cn);break;case 6: printf(Dn);break;default: printf(En);16、平面点M(x,y),若M落在圆心在坐标原点单位圆上,key=1;圆外,key=2;圆
10、内key=0#include#includevoid main()float x,y,r;int key;printf(input x,y:); scanf(%f,%f,&x,&y);r=sqrt(x*x+y*y);if(r1) key=2;else if(r=1) key=1;else if(r1) key=0; printf(%.1f,%.1f):%dn,x,y,key); -1,x0#includevoid main()int x,y;printf(Enter x:); scanf(%d,&x);if(x0) y=-1;elseif(x=0) y=0; elsey=1; printf(x
11、=%d,y=%dn,x,y);#includevoid main()int x,y;printf(Enter x:); scanf(%d,&x); y=-1; if(x!=0) if(x0)y=1;elsey=0; printf(x=%d,y=%dn,x,y);18、判定某年是否闰年。#includevoid main()int year,leap;printf(input the year:);scanf(%d,&year);if(year%4=0&year%100!=0)|(year%400=0) leap=1;else leap=0;if(leap=1)printf(%d is a leap yearn,year);elseprintf(%d is not a leap yearn,year);#includevoid main()int year,leap;printf(input the year:);scanf(%d,&year);if(year%4=0)if(year%100!=0)leap=1;elseif(year%400=0)leap=1;elseleap=0;elseleap=0;if(leap=1)printf(%d is a leap yearn,year);elseprintf(%d is not a leap year