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 seco
2、nd 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()prin
4、tf(“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
5、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;h
6、ours = 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 disp
8、lay 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 mai
9、n()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 diam
10、eter 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(“
11、%f“,printf(“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
12、 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 pr
13、ints 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“);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 state
14、ment and simple “while” repetition statement#includeint main()int grade;printf(“Please enter the grade:“);scanf(“%d“,if (grade = 60 printf(“Please enter 3 integer numbersn“);printf(“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“);
15、return 0;. .1. 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
16、 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
17、 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.00E
18、nter 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 Number Retail Price (in pounds)1 2.982 4.503 9.984 4.495 6.87Write a C program th
19、at 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
20、 given week (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(“22
21、nd“);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 “);#include int main()int a,b;a=0;b=2;whi
22、le (bint main()float a,b; while (a0 ) printf(“Enter sales in pounds (-1 to end):“);scanf(“%f“,. .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;c
23、ase 12: printf(“December “);break;printf(“19%d“,year);return 0;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 pri
24、nts 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/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
25、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 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
26、 (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;doa+;b=b*2;printf(“%d“,b);while (a
27、int main()#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=1;j=4;while(row=1;a=a-1)printf(“ “);for(b=1;b=1;a=a-1)printf(“ “);printf(“n“);row+;j-;. .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_nearest to round each of these numbers to the nearest