C语言全部题目及答案.doc_第1页
C语言全部题目及答案.doc_第2页
C语言全部题目及答案.doc_第3页
C语言全部题目及答案.doc_第4页
C语言全部题目及答案.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 second 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 %).(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()printf(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()printf(%d%d%d%d,1,2,3,4);return 0;#includeint main()float days,minutes;days = 15;minutes = days * 24 * 60;printf(The number of minutes in 15 days are %fn, minutes);return 0;#includeint main()float minutes,hours;minutes = 180;hours = minutes / 60;printf(180 minutes equal to %f hoursn, hours);return 0;#includeint main()float minutes,hours;minutes = 174;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 + 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 “p”.3Write a program that reads in two numbers: an account balance and an annual interest rate expressed as a percentage. Your program should then display 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()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,&radius);printf(The diameter 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,&SB);printf(Please enter the Annual interest rate percentage:);scanf(%f,&percentage);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 between 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 the largest in the group.ANSWER:#includeint main() int grade; printf(Please enter the grade:); scanf(%d,&grade); if (grade = 60 & grade = 0 & grade 60) printf(Failed.); else printf(Error.); return 0;#includeint main() int a,b,c; printf(Please enter 3 integer numbersn); printf(Then Ill tell you which is the largestn); scanf(%d%d%d,&a,&b,&c); if (a b & a c) printf(%d is the largest,a); else if (b a & b c) printf(%d is the largest,b); else if (c a & c b) printf(%d is the largest,c); else printf(Theyre equal); return 0;#include#includeint main() int a; printf(Please enter an integer numbern); printf(Then Ill tell you whether its an odd or even number); scanf(%d,&a); if (a%2 = 0) printf(%d is an even number,a); else printf(%d is a odd number,a); return 0;Exercise 4: switch statement and simple “while” repetition statement1. 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 while 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 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 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 NumberRetail Price (in pounds)12.9824.5039.9844.4956.87Write a C program that reads in 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 (7days).ANSWER:#includeint main() printf(Please enter three numbers for date:); int day,month,year; scanf(%d %d %d,&day,&month,&year); if(day31) printf(Error); else switch (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; case 23:printf(23rd);break; case 31:printf(31st);break; default:printf(%dth,day); switch(month) case 1: printf(January ); break; case 2: printf(February ); break; case 3: printf(March ); break; case 4: printf(April ); break; case 5: printf(May ); break; case 6: printf(June ); break; case 7: printf(July ); break; case 8: printf(August ); break; case 9: printf(September ); break; case 10: printf(October ); break; case 11: printf(November ); break; case 12: printf(December ); break; printf(19%d,year); return 0;#include int main() int a,b; a=0; b=2; while (b=30) a=a+b; b=b+2; printf(The sum of the even integers from 2 to 30 is %d,a); return 0;#includeint main() float a,b; while (a0 ) printf(Enter sales in pounds (-1 to end):); scanf(%f,&a); b=200+a*0.09; if (a=-1) printf( ); else printf(Salary is %.0fn,b); return 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 p can be calculated by the infinite series:p = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +.Write a C program that uses a do/while loop to calculate p 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 p as being 4 - 4/3 + 4/5.Nested repetition 3. Write a program 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*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; do a+; b=b*2; printf(%d,b); while (a=9); return 0;#includeint main() int a; for(a=2;a=1024;a=a*2) printf(%d,a); return 0;#include int main() double c,pie,p; int a,b,d,n; printf(Enter terms:); scanf(%d,&a); printf(Pie=); n=1;p=0; while(n1&n=a) if(n%2!=0) printf(+);else printf(-); printf(4/%d,d); n+; printf(n=%f,p);return 0;#include int main()int row,a,b,j; row=1; j=4; while(row=1;a=a-1) printf( ); for(b=1;b=1;a=a-1) printf( ); printf(n); row+; j-; row=1; j=1; while(row=5) for(a=1;a=j;a=a+1) printf( ); for(b=1;b=9-2*j;b+) printf(*); for(a=1;a=j;a=a+1) printf( ); printf(n); row+; j+; return 0;#include int main () int i, j; for (i=1; i=9; i+) for (j=1; j=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_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 function 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 time.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 receipts 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 number:); scanf(%f,&num); round_to_nearest(num); return 0;void round_to_nearest(float num1) int near_integer; int small_integer=floor(num1); if (num1-small_integer=0.5) near_integer=ceil(num1); else near_integer=floor(num1); printf(nThe nearest integer of the number is:%dn,near_integer);#includeint main() float larger_Number(float,float); float num1,num2,total=0; int a=0; while (a=num2) printf(%f,num1); larger=num1; else printf(%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 three cars parking hours:); scanf(%f%f%f,&hour1,&hour2,&hour3); 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+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 & hour19 & hour=24) charge=10; return (charge);Exercise 7: More Functions1.Write a program that uses sentinel-controlled repetition to take an integer as input, and passes it to a function even which uses the modulus operator to determine if the integer is even. The function even should return 1 if the integer is even, and 0 if it is not. The program should take the value returned by the function even and use it to print out a message announcing whether or not the integer was even.2.Write a C program that uses the function integerPower1(base, exponent) to return the value of:baseexponentso that, for example, integerPower1(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponent is a positive, non-zero integer, and base is an integer. The function should use a for loop, and make no calls to any math library functions.3.Write a C program that uses the recursive function integerPower2(base, exponent) to return the value of:baseexponentso that, for example, integerPower2(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponent is a positive, non-zero integer, and base is an integer. The function should make no calls to any math library functions.(Hint: the recursive step will use the relationship:baseexponent = base . baseexponent - 1and the base case will be when exponent is 1 since : base1 = base.)ANSWER:#includeint main() int a,b; int judge(int); void judge1(int); printf(Please enter a number); scanf(%d,&a); while(a=-1) b=judge(a); judge1(b); printf(Please evter a number); scanf(%d,&a); return 0;int judge(int x) if (x%2!=0) return (0); else return (1);void judge1(int x) if (x=1) printf(Its evenn); else printf(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,&base); printf(Enter a non-zero integer number as the exponent:n); scanf(%d,&exponent); result=integerPower2(base,exponent); printf(The power is:%d,result);return 0;int integerPower2(int x,int y) if(y=1) return (x); else return (x*integerPower2(x,y-1);#include int main() int integerPower1(int,int); int base,exponent,answer; printf(Let us calculate the powern); printf(Please enter a integer base number:n); scanf(%d,&base); printf(Please enter a non-zero integer number as the exponent:n); scanf(%d,&exponent); answer=integerPower1(base,exponent); printf(So the power is:%d,answer);return 0;int integerPower1(int x,int y) int i,a; a=1; for(i=1;i=y;i+) a=x*a; return (a);Exercise 08: Arrays1. Write a program that reads ten numbers supplied by the user into a single subscripted array, and then prints out the average of them.2.Write a program that reads ten numbers supplied by the user into a 2 by 5 array, and then prints out the maximum and minimum values held in:(a) each row (2 rows)(b) the whole array3 Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论