收藏 分享(赏)

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

上传人:还是太帅 文档编号:9087051 上传时间:2019-07-23 格式:DOC 页数:19 大小:80KB
下载 相关 举报
C语言-全部题目及其答案.doc_第1页
第1页 / 共19页
C语言-全部题目及其答案.doc_第2页
第2页 / 共19页
C语言-全部题目及其答案.doc_第3页
第3页 / 共19页
C语言-全部题目及其答案.doc_第4页
第4页 / 共19页
C语言-全部题目及其答案.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

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营业执照举报