ImageVerifierCode 换一换
格式:DOC , 页数:19 ,大小:80KB ,
资源ID:9087051      下载积分:10 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.docduoduo.com/d-9087051.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录   微博登录 

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C语言-全部题目及其答案.doc)为本站会员(还是太帅)主动上传,道客多多仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知道客多多(发送邮件至docduoduo@163.com或直接QQ联系客服),我们立即给予删除!

C语言-全部题目及其答案.doc

1、C 语言全部题目及答案Exercise 1: Programming Environment and Basic Input/Output1. Write a program that prints “This is my first program!” on the screen. (a) Save this program onto your own disk with the name of e2-1a;(b) Run this program without opening Turbo C;(c) Modify this program to print “This is my sec

2、ond program!”, then save it as e2-1b. Please do not overwrite the first program.2. Write a program that prints the number 1 to 4 on the same line. Write the program using the following methods:(a) Using four “printf” statements.(b) Using one “printf” statement with no conversion specifier (i.e. no %

3、).(c) Using one “printf” statement with four conversion specifiers3(a) Write a program that calculates and displays the number of minutes in 15 days.(b) Write a program that calculates and displays how many hours 180 minutes equal to.(c) (Optional) How about 174 minutes?ANSWERS:#includeint main()pri

4、ntf(“This is my first program!“);return 0;#includeint main()printf(“This is my second program!“);return 0;#includeint main()printf(“1“);printf(“2“);printf(“3“);printf(“4“);return 0;#includeint main()printf(“1234“);return 0;#includeint main()float days,minutes;days = 15;minutes = days * 24 * 60;print

5、f(“The number of minutes in 15 days are %fn“, minutes);return 0;#includeint main()float minutes,hours;minutes = 180;#includeint main()printf(“%d%d%d%d“,1,2,3,4);return 0;hours = minutes / 60;printf(“180 minutes equal to %f hoursn“, hours);return 0;#includeint main()float minutes,hours;minutes = 174;

6、hours = minutes / 60;printf(“174 minutes equal to %f hoursn“, hours);return 0;Exercise 2: Data Types and Arithmetic Operations1. You purchase a laptop computer for $889. The sales tax rate is 6 percent. Write and execute a C program that calculates and displays the total purchase price (net price +

7、sales tax).2Write a program that reads in the radius of a circle and prints the circles diameter, circumference and area. Use the value 3.14159 for “”.3Write a program that reads in two numbers: an account balance and an annual interest rate expressed as a percentage. Your program should then displa

8、y the new balance after a year. There are no deposits or withdraws just the interest payment. Your program should be able to reproduce the following sample run:Interest calculation program.Starting balance? 6000Annual interest rate percentage? 4.25Balance after one year: 6255ANSWER:#includeint main(

9、)float net_price,sales_tax,total;net_price = 889;sales_tax = net_price * 0.06;total = net_price + sales_tax;printf(“The total purchase price is %g“, total);return 0;#includeint main()printf(“Please input a number as radius:n“);float radius,diameter,circumference,area;scanf(“%f“,printf(“The diameter

10、is %gn“,diameter = radius * 2);printf(“The circumference is %gn“,circumference = radius * 2 * 3.14159);printf(“The area is %gn“, area = radius * radius * 3.14159);return 0;#includeint main()float SB,percentage,NB;printf(“Interest calculation programnnPlease enter the Starting Balance:“);scanf(“%f“,p

11、rintf(“Please enter the Annual interest rate percentage:“);scanf(“%f“,NB = SB * percentage / 100 + SB;printf(“nThe Balance after one year is:%g“,NB);return 0;Exercise 3: Selection structure1 Write a C program that accepts a students numerical grade, converts the numerical grade to Passed (grade is b

12、etween 60-100), Failed (grade is between 0-59), or Error (grade is less than 0 or greater than 100).2 Write a program that asks the user to enter an integer number, then tells the user whether it is an odd or even number. 3 Write a program that reads in three integers and then determines and prints

13、the largest in the group.ANSWER:#include#includeint main()int a;printf(“Please enter an integer numbern“);printf(“Then Ill tell you whether its an odd or even number“);#includeint main()int grade;printf(“Please enter the grade:“);scanf(“%d“,if (grade = 60 printf(“Please enter 3 integer numbersn“);pr

14、intf(“Then Ill tell you which is the largestn“);scanf(“%d%d%d“,if (a b else if (b a else if (c a elseprintf(“Theyre equal“);return 0;scanf(“%d“,if (a%2 = 0)printf(“%d is an even number“,a);elseprintf(“%d is a odd number“,a);return 0;Exercise 4: switch statement and simple “while” repetition statemen

15、t1. Write a program that reads three integers an abbreviated date (for example: 26 12 94) and that will print the date in full; for example: 26th December 1994. The day should be followed by an appropriate suffix, st, nd, rd or th. Use at least one switch statement.2Write a C program that uses a whi

16、le loop to calculate and print the sum of the even integers from 2 to 30.3. A large chemical company pays its sales staff on a commission basis. They receive 200 per week plus 9% of their gross sales for that week. For example, someone who sells 5000 of chemicals in one week will earn 200 plus 9% of

17、 5000, a total of 650. Develop a C program that will input each salespersons sales for the previous week, and print out their salary. Process one persons figures at a time.Enter sales in pounds (-1 to end): 5000.00Salary is: 650.00Enter sales in pounds (-1 to end): 00.00Salary is: 200.00Enter sales

18、in pounds (-1 to end): 1088.89Salary is: 298.00Enter sales in pounds (-1 to end): -1Optional:4. A mail order company sells five different products whose retail prices are shown in the following table:Product Number Retail Price (in pounds)1 2.982 4.503 9.984 4.495 6.87Write a C program that reads in

19、 a series of pairs of numbers as follows:(1). Product number(2). Quantity sold for one dayYour program should use a switch statement to help determine the retail price for each product, and should use a sentinel-controlled loop to calculate the total retail value of all products sold in a given week

20、 (7days).ANSWER:#includeint main()printf(“Please enter three numbers for date:“);int day,month,year;scanf(“%d %d %d“,if(day31)printf(“Error“);elseswitch (day)case 1:printf(“1st“);break;case 2:printf(“2nd“);break;case 3:printf(“3rd“);break; case 21:printf(“21st“);break;case 22:printf(“22nd“);break;ca

21、se 23:printf(“23rd“);break;case 31:printf(“31st“);break;default:printf(“%dth“,day);switch(month)#include int main()int a,b;a=0;b=2;while (bint main()float a,b; while (a0 ) printf(“Enter sales in pounds (-1 to end):“);scanf(“%f“,b=200+a*0.09;if (a=-1)printf(“ “);else printf(“Salary is %.0fn“,b); retu

22、rn 0;Exercise 5: for and do while” repetition statements 1. Write a program which uses a do/while loop to print out the first 10 powers of 2 other than 0 (ie. it prints out the values of 21, 22, ., 210). Use a for loop to do the same.2. The constant can be calculated by the infinite series: = 4 - 4/

23、3 + 4/5 - 4/7 + 4/9 - 4/11 +Write a C program that uses a do/while loop to calculate using the series. The program should ask the user how many terms in the series should be used. Thus if the user enters 3, then the program should calculate as being 4 - 4/3 + 4/5.Nested repetition 3. Write a program

24、 that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of printf statements.*4. Write a program to print a table as follows:1*1= 12*1= 2 2*

25、2= 43*1= 3 3*2= 6 3*3= 9.9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81ANSWER:#includeint main()int a,b;a=0;b=1;doa+;b=b*2;printf(“%d“,b);while (a#include int main()double c,pie,p;int a,b,d,n;printf(“Enter terms:“);scanf(“%d“,printf(“Pie=“);n=1;p=0;while(nint main()int row,a,b,j;row=

26、1;j=4;while(row=1;a=a-1)printf(“ “);for(b=1;b=1;a=a-1)printf(“ “);printf(“n“);row+;j-;int main()int a;for(a=2;a1for (i=1; i=j)printf(“%1d*%1d=%2d “, i, j, i*j);printf(“n“); getchar();return 0;Exercise 6: Simple Functions 1. Write a C program that reads several numbers and uses the function round_to_

27、nearest to round each of these numbers to the nearest integer. The program should print both the original number and the rounded number. 2. Write a program that reads three pairs of numbers and adds the larger of the first pair, the larger of the second pair and the larger of the third pair. Use a f

28、unction to return the larger of each pair.3. A car park charges a 2.00 minimum fee to park for up to 3 hours, and an additional 0.50 for each hour or part hour in excess of three hours. The maximum charge for any given 24-hour period is 10.00. Assume that no car parks for more than 24 hours at a tim

29、e.Write a C program that will calculate and print the parking charges for each of 3 customers who parked their car in the car park yesterday. The program should accept as input the number of hours that each customer has parked, and output the results in a neat tabular form, along with the total rece

30、ipts from the three customers:Car Hours Charge1 1.5 2.002 4.0 2.503 24.0 10.00TOTAL 29.5 14.50The program should use the function calculate_charges to determine the charge for each customer.ANSWER:#include#includeint main()void round_to_nearest(float); float num;while (5)printf(“Please input a numbe

31、r:“);scanf(“%f“,round_to_nearest(num);return 0;#includeint main()float larger_Number(float,float);float num1,num2,total=0;int a=0;while (a=0.5)near_integer=ceil(num1);elsenear_integer=floor(num1);printf(“nThe nearest integer of the number is:%dn“,near_integer);printf(“nThe total is %f“,total);return

32、 0;float larger_Number(float num1,float num2)float larger;if (num1=num2)printf(“%f“,num1);larger=num1;elseprintf(“%f“,num2);larger=num2;printf(“n“);return (larger);#include int main()float calculate_charges(float);float hour1,hour2,hour3,charge1,charge2,charge3,total1=0,total2=0;printf(“Please input

33、 three cars parking hours:“);scanf(“%f%f%f“,charge1=calculate_charges(hour1);charge2=calculate_charges(hour2);charge3=calculate_charges(hour3);printf(“Car Hours Chargen“);printf(“1%10.1f%10.2fn“,hour1,charge1);printf(“2%10.1f%10.2fn“,hour2,charge2);printf(“3%10.1f%10.2fn“,hour3,charge3);total1=hour1

34、+hour2+hour3;total2=charge1+charge2+charge3;printf(“TOTAL%7.2f%9.2f“,total1,total2);return 0;float calculate_charges(float hour)float charge;if (hour3 int judge(int);void judge1(int);printf(“Please enter a number“);scanf(“%d“,while(a=-1)b=judge(a);judge1(b);printf(“Please evter a number“);scanf(“%d“

35、,return 0;int judge(int x)if (x%2!=0)return (0);elsereturn (1);void judge1(int x)if (x=1)printf(“Its evenn“);elseprintf(“Its oddn“);#includeint main()int integerPower2(int,int);int base,exponent,result;printf(“This program can calculate the powern“);printf(“Enter a integer base number:n“);scanf(“%d“

36、,printf(“Enter a non-zero integer number as the exponent:n“);scanf(“%d“,result=integerPower2(base,exponent);printf(“The power is:%d“,result);return 0;int integerPower2(int x,int y)if(y=1)return (x);elsereturn (x*integerPower2(x,y-1);#include int main()int integerPower1(int,int);int base,exponent,ans

37、wer;printf(“Let us calculate the powern“); printf(“Please enter a integer base number:n“);scanf(“%d“,printf(“Please enter a non-zero integer number as the exponent:n“);scanf(“%d“,answer=integerPower1(base,exponent);printf(“So the power is:%d“,answer);return 0;int integerPower1(int x,int y)int i,a;a=

38、1;for(i=1;iint main()#define MAXGRADES 10int gradesMAXGRADES;int i, total = 0;float average;for (i=0;iint main()#define MAXNUM 5int numberMAXNUM;int i,j,a;printf(“Please enter 5 numbers between 10 and 100n“); printf(“Enter a number:“);scanf(“%d“,printf(“nThe average of the numbersn“);for (i=0;iint m

39、ain()#define NUMROWS 2#define NUMCOLS 5int numberNUMROWSNUMCOLS;int i,j,max1,max2,min1,min2;for (i=0;i=max1)max1=number0j;if(number0j=max2)max2=number1j;if(number1j=max2)printf(“%dn“,max1);else printf(“%dn“,max2);printf(“The min of two rows is “);if (min1int main()char character520;int integer5;int

40、i;for(i=0;iint main()char four481;int i;printf(“Please input four words:n“);for(i=0;i=0;i-) printf(“%s“,fouri);return 0;/naP reteP ma I#include int main()char four481;int i,j;printf(“Please input four words:n“);for(i=0;i=0;i-)for(j=0;fourij!=0;j+);for(j=j-1;j=0;j-)printf(“%c“,fourij);printf(“ “);ret

41、urn 0;Exercise 10: Pointers1. Write a program that reads 5 integers into an array, and then uses four different methods of accessing the members of an array to print them out in reverse order.2. Write a program that reads 8 floats into an array and then prints out the second, fourth, sixth and eight

42、h members of the array, and the sum of the first, third, fifth and seventh, using pointers to access the members of the array.3. (Optional) Write a program that use a SINGLE FUNCTION (用一个函数)to find and return simultaneously both the lowest and highest values in an array of type int. Suppose the size

43、 of the array is 6. ANSWER:#includeint main()int num15,i;printf(“Please input 5 numbers:n“);for (i=0;i=0;i-)printf(“%d“,num1i);/the second wayprintf(“nPrint them out in reverse order in the second way:n“);int *num2;num2 = for (i=4;i=0;i-)printf(“%d“,*(num2+i);/the third wayprintf(“nPrint them out in

44、 reverse order in the third way:n“);for(i=4;i=0;i-)printf(“%d“,*(num1+i);/the fourth wayprintf(“nPrint them out in reverse order in the fourth way:n“);int *num4;num4 = for (i=0;iint main()float num8;int i;printf(“Please input 8 floats:n“);for (i=0;ivoid SingleFunction(int *,int *);int main() int num6;int i;printf(“Please input 6 numbers:n“);for (i=0;i *(min+i)minnum = *(min+i);printf(“The min is: %d“,minnum);

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报