计算机程序设计题怎么做,计算机程序设计基础(C语言)编程习题

《计算机程序设计基础(C语言)编程习题》由会员分享,可在线阅读,更多相关《计算机程序设计基础(C语言)编程习题(20页珍藏版)》请在人人文库网上搜索。

1、计算机程序设计基础(c语言) 习题 编程题计算机程序设计基础(C语言)编程练习题及参考答案1.输入2个整数,求两数的平方和并输出。 #include main() intt a ,b,s; printf(please input a,b:n);scanf(%d%d”,&a,&b);s=a*a+b*b;printf(the result is %dn,s); 2. 输入一个圆半径(r)当r0时,计算并输出圆的面积和周长,否则,输出提示信息。#include #define PI 3.14 main() float r ,s , l; printf(please input r:n);scanf(。

2、%f”,&r);if (r=0) s=pi*r*r;l=2*i*r ;printf(the area is %fn,s);printf(the circumference is %fn,l);else printf(input error!n); 3、函数y=f(x)可表示为:2x+1 (x0) 编程实现输入一个x值,输出y值。 main()int x,y;scanf(“%d”,&x);If(x0)y=2*x-1;If(x=0) y=0;printf(“%d”,y);4、编写一个程序,从4个整数中找出最小的数,并显示此数。main( )int a,b,c,d,t;scanf (“%d,%d,%。

3、d,%d ”,&a,&b,&c,&d);if (ab)t=a; a=b; b=t;if (ac)t=a; a=c; c=t;if (ad)t=a; a=d; d=t;printf (“min = %d n”,a);5有一函数当x0时,y=3,当x=0时y=5,编程,从键盘输入一个x值,输出y值。main()int x,y;scanf(%d,&x);if (xmain()int yourAge, hisAge;printf(Please enter your age:);scanf(%d, &yourAge); /*输入你的年龄yourAge*/printf(Please enter your 。

4、friends age:);scanf(%d, &hisAge); /*输入你朋友的年龄hisAge*/if (yourAge = hisAge) printf(You are older! Your age is = %dn, yourAge); if (hisAge yourAge)printf(Your friend is older! HisAge age is = %dn, hisAge); 8、键盘输入2个加数,再输入答案,如果正确,显示“right”,否则显示“error”#include “stdio.h”main( )int a,b,c;printf(“please inpu。

5、t a and bn”);scanf (%d,%d”,&a,&b);printf(“please input the answer for a+bn”);scanf (%d”,&c);if (c=a+b)printf(“rightn”);elseprintf(“errorn”);9. 编一程序每个月根据每个月上网时间计算上网费用,计算方法如下:要求当输入每月上网小时数,显示该月总的上网费用(6分)main() int hour;float fee;printf(“please input hour:n”);scanf(“%d”,&hour);if(hour=10&hour=y)printf(“。

6、建议使用全球通”);else printf(“建议使用神州行);11个人所得税计算,应纳税款的计算公式如下:收入税率收入收入1000元的部分53000元收入2000元的部分106000元收入3000元的部分15收入6000元的部分20输入某人的收入,计算出应纳税额及实际得到的报酬。(7分)(如需连续计算多个人的纳税情况,直到输入负数为止,程序应如何改进?试写出程序)#include “stdio.h”main()int grade; float income,tax,money;printf(“please input your incomen”);scanf (“%f”,&income);i。

7、f (incomemain()int data; char grade; printf(Please enter the score:);scanf(%d”, &data); switch(data/10) case 10: case 9 : grade=A; break; case 8: grade=B; break;case 7: grade=C; break;case 6: grade=D; break; default: grade=E;printf(the grade is %c”,grade);*13. 编程设计一个简单的计算器程序。从键盘输入2个操作数,1个运算符,当运算符为加(。

8、+)、减(-)、乘(*)、除(/)时,输出计算结果#include main() int data1, data2; /*定义两个操作符*/char op; /*定义运算符*/printf(Please enter the expression:);scanf(%d%c%d, &data1, &op, &data2); /*输入运算表达式*/switch(op) /*根据输入的运算符确定要执行的运算*/ case +: /*处理加法*/printf(%d + %d = %d n, data1, data2, data1 + data2); break;case -: /*处理减法*/print。

9、f(%d - %d = %d n, data1, data2, data1 - data2);break;case *: /*处理乘法*/printf(%d * %d = %d n, data1, data2, data1 * data2); break;case /: /*处理除法*/if (0 = data2) /*为避免出现溢出错误,检验除数是否为0*/printf(Division by zero!n);else printf(%d / %d = %d n, data1, data2, data1 / data2); break;default: printf(Unknown oper。

10、ator! n);14. 从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。main( )int a10, i,p=0,n=0,z=0;printf(“please input number”); for(i=0;i0)p+;else if (aimain( ) int i, sum=1for(i=1; imain( ) int i, sum=0;for(i=1; imain()int i , j , k; for (i = 1; i #define M 30main ( ) float scoreM, max , min, aver;int i ;printf(“pleas。

11、e input score: n”);for(i=0; iscorei) min=scorei;aver+=scorei;printf(“max=%f, min=%f,aver=%f”, max, min, aver/M);24. 从键盘输入某班学生某门课的成绩及其学号(班级人数最多40人,具体人数由键盘输入),输出该班最高分和最低分及其学生学号;并输出该班该课程的总分和平均分。请编写程序。#include #define ARR_SIZE 40main() float scoreARR_SIZE, maxScore,minScore,sum;int n, i;long maxNum, min。

12、Num,numARR_SIZE;printf(Please enter total number:);scanf(%d, &n); printf(Please enter the number and score:n);for (i=0; i maxScore) maxScore = scorei; maxNum = numi; else if (scorei y)t=x;x=y;y=t;while(zy)t=x;x=y;y=t;z=x;while(z1) if(x%z=0)&(y%z=0) break;z-;return(z);main()int a,b,c;char ch;printf(n。

13、mingb(1)/maxgy(2)?);ch=getchar();printf(ninput:);scanf(%d,%d,&a,&b);if(ch=1) c=mingb(a,b);else if(ch=2) c=maxgy(a,b);printf(the result is %d,c);getch();*28. 输入一个3*3矩阵,求出其转置矩阵,并求出两个矩阵的和.main()int a33;int b33;int c33int i,j;printf(“please input 6 numbers!”)for (i=1;imain( )int i, j ,a53;printf(“Enter 。

14、data:n”);for(i=0;i#define N 20main( ) int aN, bN, i, j, temp;printf(“please input a:n”);for(i=0; imain() int a43, i , j ,min,m,n;printf(Please enter data:);for (i=0; i#include #define ARR_SIZE 80main()char strARR_SIZE;int len, i, letter = 0, digit = 0, space = 0, others = 0;printf(Please input a str。

15、ing:);gets(str);len = strlen(str); for (i=0; i= a & stri = A & stri = 0 & stri #define ARR_SIZE 40void Sort(float score, long num, int n); main() float scoreARR_SIZE;int n, i;long numARR_SIZE;printf(Please enter total number:);scanf(%d, &n); printf(Please enter the number and score:n);for (i=0; i sc。

16、orei) temp1 = scorej;scorej = scorei; scorei = temp1;/*交换学号*/temp2 = numj; numj = numi; numi = temp2; *35编程实现如下功能:实现从键盘输入两个字符串,分别存入两个不同的字符数组中;将两个字符串连接为一个字符串,并打印输出连接后的整个字符。#include #include #define ARR_SIZE 80void MyStrcat(char dstStr, char srcStr);main() char sARR_SIZE, tARR_SIZE;printf(Please enter。

17、 source string: );gets(s);printf(Please enter destination string: );gets(t);MyStrcat(s,t);printf(The concatenate string is: );puts(s);void MyStrcat(char dstStr, char srcStr) int i = 0, j;while (dstStri != 0) i+;for (j=0; srcStrj!=0; j+, i+)dstStri = srcStrj;dstStri = 0;*36、猜数游戏。系统随机产生一个整数,通过键盘输入数据猜数。

18、,猜对为止,并要求统计猜的次数。注:rand()函数可以产生032767间的正整数,程序中需包含stdlib.h。#include #include main() int magic; int guess; int counter; magic = rand() % 100 + 1; counter = 0; doprintf(Please guess a magic number:);scanf(%d, &guess); counter +; if (guess magic) printf(Wrong!Too high!n);else if (guess void Swap(int *x, 。

19、int *y);main() int a, b;printf(Please enter a,b:);scanf(%d,%d, &a, &b); printf(Before swap: a = %d,b = %dn, a,b); Swap(&a, &b); printf(After swap: a = %d,b = %dn, a, b); void Swap(int *x, int *y) int temp;temp = *x; *x = *y; *y = temp; 38.随机输入若干个学生的体重,以输入负数或零结束,分别求最重和最轻的体重,并计算平均体重。 #include main() i。

20、nt n=0;float weight,max=0,min=10,sum=0,ave;printf(“please input the weight:”);scanf(“%f”,& weight);while(weight0) sum=weight+sum;n+;if (weightmax)max=weight;scanf(“%f”,& weight);if (n0) ave=sum/n; printf(maxweight = %fn , max); printf(minweight = %fn, min);printf(ave = %fn,ave);else printf(NO VALID 。

21、DATA”);39.输入m,k的值,编程求下面表达式的值:(要求编写一个求阶乘的函数,调用函数实现本题)#include unsigned long Factorial(unsigned int number);main() unsigned int m, k;double p;printf(Please input m, k:);scanf(%u, %u, &m, &k);p = (double)Factorial(k) / Factorial (m-k);printf(p=%fn, p);unsigned long Factorial(unsigned int number)unsigne。

22、d long i, result = 1;for (i=2; iint IsPrimeNumber(int number)int i;if (number = 1)return 0;for (i=2; isqrt(number); i+)if (number % i) = 0)return 0; return 1;main() int n;printf(“Please input n:”);scanf(“%d”,&n);if(IsPrimeNumber(n)printf(“n%d is a Prime Number”,n);else printf(“n%d is not a Prime Number”,n);20。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值