C語言全部題目及答案_第1頁
C語言全部題目及答案_第2頁
C語言全部題目及答案_第3頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

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 second

2、program! ”, then save itas e2-1b. Please do not overwrite the first program.2. Write a program that prints the number 1 to 4 on the samel ine. Write the program using the following methods:(a) Using four “printf ” stateme nts.(b) Using one “printf ” statement with no conversion specifier (i.e. no %&

3、#39;).(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:#include<stdi

4、o.h>int main()printf("This is my first program!");return 0;#include<stdio.h>int main()printf("This is my second program!");return 0;#include<stdio.h> int main()printf("1");printf("2");printf("3");printf("4"); return 0;#includ

5、e<stdio.h> int main() printf("1234");return 0; #include<stdio.h> int main()printf("%d%d%d%d",1,2,3,4); return 0;#include<stdio.h>int main()float days,minutes; days = 15;minutes = days * 24 * 60;printf("The number of minutes in 15 days are %fn", minutes

6、); return 0;#include<stdio.h>int main()float minutes,hours; minutes = 180;hours = minutes / 60; printf("180 minutes equal to %f hoursn", hours);return 0;#include<stdio.h> int main() float minutes,hours; minutes = 174;hours = minutes / 60; printf("174 minutes equal to %f ho

7、ursn", 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).2 Write a program that reads in the rad

8、ius of a circle andprints the circle'sdiameter, circumference and area. Use the value 3.14159 for“ ”.3 Write 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

9、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: #include<stdio.h> int main() float net_price,sales_tax,t

10、otal;net_price = 889;sales_tax = net_price * 0.06;total = net_price + sales_tax; printf("The total purchase price is %g", total); return 0;#include<stdio.h>int main()printf("Please input a number as radius:n");float radius,diameter,circumference,area;scanf("%f",&a

11、mp;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;#include<stdio.h>int main()float SB,percentage,NB;printf(&qu

12、ot;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",

13、NB);return 0;Exercise 3: Selection structure1 Write a C program that accepts a student' s numerical grade, converts thenumerical 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

14、enter an integer number, then tellsthe 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:#include<stdio.h>#include<stdio.h>#include<stdio.h>int main()int grade;printf("Please e

15、nter the grade:");scanf("%d",&grade);if (grade >= 60 && grade <=100)printf("Passed.");else if (grade >= 0 && grade<60)printf("Failed.");elseprintf("Error.");return 0;int mai n() int a;pri ntf ("P lea se ent er an int e

16、ge r num ber#include<stdio.h> int main() int a,b,c; printf("Please enter 3 integer numbersn");printf("Then I'll tell you which is the largestn");scanf("%d%d%d",&a,&b,&c); if (a > b && a > c) printf("%d is the largest",a); el

17、se 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("They're equal"); return 0;n");printf("Then I'll tell you whether it's an odd or even number"

18、); 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 ” repetitionstatement1. Write a program that reads three integers an abbreviated date (for example:26 12

19、 94) and that will print the date in full; for example: 26th December1994. The day should be followed by an appropriate suffix, st ', nd' ,rd' or th'. Use at least oneswitch statement.2 Write a C program that uses a while loop to calculate and print the sum of theeven integers from 2

20、 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 sa

21、lesperson ' s sales for the previous week, and print out their salary. Process one person 's 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

22、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)1 2.982 4.503 9.984 4.495 6.87Write a C program that reads in a series of pairs of numbers as follows:(1). Product

23、 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:#include<stdio.h>#include <s

24、tdio.h>int main()int main()printf("Please enter three numbers forint a,b;date:");a=0;int day,month,year;b=2;scanf("%d %d %d",&day,&month,&year);while (b<=30)if(day>31)printf("Error");a=a+b;elseb=b+2;switch (day)printf("The sum of the evenintege

25、rs from 2 to 30 is %d",a);case 1:printf("1st");return 0;break;case 2:printf("2nd");#include<stdio.h>break;int main()case 3:printf("3rd");break;case 21:printf("21st");break;case 22:printf("22nd");break;case 23:printf("23rd");break

26、;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 ");brea

27、k;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 "); b

28、reak;printf("19%d",year);return 0; float a,b; while (a>0 ) 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 ” repetitio

29、n statements1. Write a program which uses a do/while loop to print out the first 10 powersof 2 other than 0 (ie. it prints out the values of 21, 2 2, ., 2 10). Use afor loop to do the same.2. The constantcan be calculated by the infinite series:= 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +Write a C program t

30、hat 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 enters3', then the program should calculateas being 4 - 4/3 +4/5.Nested repetition3. Write a program that prints the following diamond shape. You

31、may useprintfstatements that print either a single asterisk (*) or a single blank. Maximize your use of repetition (with nestedfor statements) and minimize the number ofprintf statements.*4. Write a program to print a table as follows: 1*1= 1 2*1= 2 2*2= 4 3*1= 3 3*2= 6 3*3= 99*1= 9 9*2=18 9*3=27 9*

32、4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 ANSWER:#include<stdio.h>#include <stdio.h>#include <stdio.h>int main()int main()int main()double c,pie,p;int row,a,b,j;int a,b;int a,b,d,n;row=1;a=0;printf("Enterj=4;b=1;terms:");while(row<=5)doscanf("%d",&a);for(a=

33、j;a>=1;a=a-1)printf("Pie=");printf(" ");a+;n=1;p=0;b=b*2;while(n<=a)for(b=1;b<=9-2*j;b+)printf("%d",b);printf("*");if(n%2=0)for(a=j;a>=1;a=a-1)while (a<=9);b=-1;printf(" ");return 0;elseprintf("n");b=1;row+;#include<stdio.

34、h>j-;int main()pie=(4.0*b)/(2.0*n-1.0);row=1;int a;d=2*n-1;j=1;p=p+pie;while(row<=5)for(a=2;a<=1024;a=aif(n>1&&n<=a)for(a=1;a<=j;a=a+1)*2)printf(" ");printf("%d",a);if(n%2!=0)return 0;printf("+");for(b=1;b<=9-2*j;b+)else printf("-"

35、);printf("*");for(a=1;a<=j;a=a+1)printf("4/%d",d);printf(" ");n+;printf("n");row+;printf("n=%f",p);j+;return 0;return 0; #include <stdio.h> int main ()int i, j;for (i=1; i<=9; i+)for (j=1; j<=9; j+)if (i>=j)printf("%1d*%1d=%2d

36、", i, j, i*j); printf("n");getchar();return 0;Exercise 6: Simple Functions1. 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 num

37、ber.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 anadditional 0.50 f

38、or each hour or part hour in excess of three hours. The maximumc harge for any given 24-hour period is 10.00. Assumet hat no car parks for more than 24 hours at a time.Write a C program that will calculate and print the parking charges for eachof 3 customers who parked their car in the car park yest

39、erday. 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:CarHoursCharge11.52.0024.02.50324.010.00TOTAL 29.5 14.50The program should use the functioncalculate_char

40、ges to determine thecharge for each customer.ANSWER:#include<stdio.h> #include<math.h> int 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

41、near_integer;intsmall_integer=floor(num1);if (num1-small_integer>=0.5) near_integer=ceil(num1);elsenear_integer=floor(num1);printf("nThe nearest integer of the number is:%dn",near_integer);#include<stdio.h>int main()float larger_Number(float,float); float num1,num2,total=0;int a=0

42、; while (a<3) printf("Please input two numbers:");scanf("%f%f",&num1,&num2);total=total+larger_Number(num1,num2)a=a+1; printf("nThe total is %f",total); return 0;float larger_Number(float num1,float num2)float larger;if (num1>=num2) printf("%f",n

43、um1); larger=num1; else printf("%f",num2); larger=num2; printf("n"); return (larger);#include <stdio.h>int main()float calculate_charges(float);float hour1,hour2,hour3,charge1,charge2,charge3,total1=0,total2=0; printf("Please input three car's parking hours:")

44、;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);

45、 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 (hour<=3)charge=2;if (hour>3 && hour<=19) charge=2.+0.50*(hour-

46、3.);if (hour>19 && hour<=24)charge=10;return (charge);Exercise 7: More Functions1. Write a program that uses sentinel-controlled repetition to take an integeras input, and passes it to a functioneven which uses the modulus operatorto determine if the integer is even. The function even

47、should return 1 if theinteger is even, and0 if it is not.The program should take the value returned by the functioneven and use itto print out a message announcing whether or not the integer was even.2. Write a C program that uses the functionintegerPower1(base, exponent)to return the value of:expon

48、entbaseso that, for example, integerPower1(3, 4)gives the value3 * 3 * 3 *for loop, and make no calls to any math3. Assume that exponent is a positive, non-zero integer, and base is aninteger. The function should use a library egerPower2(base,3. Write a C program that uses the recursive

49、 functionexponent) to return the value of:baseexponentso that, for example, integerPower2(3, 4) gives the value3 * 3 * 33. Assume that exponent is a positive, non-zero integer, andbase is aninteger. The function should make no calls to any math library functions.(Hint: the recursive step will use th

50、e relationship:baseexponent = base . base exponent - 1and the base case will be whenexponent is 1 since : base = base.)ANSWER:#include<stdio.h> int main() int a,b;int judge(int); void judge1(int);printf("Please enter a number");scanf("%d",&a); while(a>=-1) b=judge(a)

51、;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)#include<stdio.h>int main()int integerPower2(int,int);int base,exponent,result;printf("This program can calculate the po

52、wern");printf("Enter a integer base number:n"); scanf("%d",&base);printf("Entera non-zero integer number asthe exponent:n");scanf("%d",&exponent); result=integerPower2(base,exponent); printf("The power is:%d",result);return 0;int integer

53、Power2(int x,int y)if(y=1)return (x);elsereturn (x*integerPower2(x,y-1);#include <stdio.h>int main()int integerPower1(int,int);int base,exponent,answer;if (x=1)printf("It's evenn"); elseprintf("It's oddn");printf("Let us calculate the powern");printf("

54、;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

55、(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 arr

56、ay, 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 a duplicate o

57、f a number already read. Prepare for the “ worst case ” in which all 20 numbers are different. Use the smallestpossible array to solve this problem. #include<stdio.h> int main() #define MAXGRADES 10 int gradesMAXGRADES; int i, total = 0; float average;for (i=0;i<MAXGRADES;i+) printf("E

58、nter a number:");scanf("%d",&gradesi);printf("nThe average of the numbersn");#include<stdio.h>int main()#define MAXNUM 5int numberMAXNUM;int i,j,a;printf("Please enter 5 numbers between 10 and 100n");for (i=0;i<MAXNUM;i+) scanf("%d",&numb

59、eri);a=0; for(i=0;i<MAXNUM;i+) for(j=0;j<i;j+)for (i=0;i<MAXGRADES;i+) printf("%d",gradesi);total+=gradesi;average = total/10.0; printf("is %fn",average); return 0; if(numberi=numberj) a+;if(a=0) printf("%d",numberi);a=0;return 0;ANSWER: #include<stdio.h>

60、 int main() #define NUMROWS 2 #define NUMCOLS 5 int numberNUMROWSNUMCOLS; int i,j,max1,max2,min1,min2; for (i=0;i<NUMROWS;i+) printf("Please input 5 numbers with space between each other:n");for (j=0;j<NUMCOLS;j+) scanf("%d",&numberij); max1=number00; min1=number00; max

61、2=number10; min2=number10; for(j=0;j<NUMCOLS;j+) if(number0j>=max1) max1=number0j;if(number0j<=min1) min1=number0j;if(number1j>=max2) max2=number1j;if(number1j<=min2) min2=number1j; printf("The max of the first row is %dn",max1); printf("The min of the first row is %dn",min1); printf("The max of the second row is %dn",max2); printf("The min of the second row is %dn",min2); printf("The max of two row

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論