当前位置:文档之家› C语言全部题目及答案

C语言全部题目及答案

C语言全部题目及答案 SANY GROUP system office room 【SANYUA16H-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 thefollowing 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?Exercise 2: Data Types and Arithmetic Operations1. You purchase a laptop computer for $889. The sales tax rate is 6 percent. Write and executea C program that calculates and displays the total purchase price (net price + sales tax). 2.Write a program that reads in the radius of a circle and prints the circle’s diameter, 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 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,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;}#include<stdio.h>int main(){printf("Please input a number as radius:\n");float radius,diameter,circumference,area;scanf("%f",&radius);printf("The diameter is %g\n",diameter = radius * 2);printf("The circumference is %g\n",circumference = radius * 2 * 3.14159);printf("The area is %g\n", area = radius * radius * 3.14159);return 0;}#include<stdio.h>int main(){float SB,percentage,NB;printf("Interest calculation program\n\nPlease 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 student’s 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:#include<stdio.h>#include<stdio.h>{int a;printf("Please enter an integer number\n");printf("Then I'll tell you whether it's an odd or even number");scanf("%d",&a);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 statement1. Write a program that reads three integers an abbreviated date (for example: 26 12 94) andthat will print the date in full; for example: 26th December 1994. The day should befollowed by an appropriate suffix, ‘st’, ‘nd’, ‘rd’ or ‘th’. Use at least one switchstatement.2.Write 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 £200per 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 salesperson’s sales for the previous week, and print out t heir 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 sales in pounds (-1 to end): -1Optional:4. A mail order company sells five different products whose retail prices are shown in thefollowing table:Product Number Retail 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 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:Exercise 5: ‘for’ and ‘do … while” repetition statements1. Write a program which uses a do/while loop to print out the first 10 powers of 2 otherthan 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/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 repetition3. 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=81int main (){int i, j;for (i=1; i<=9; i++){for (j=1; j<=9; j++)if (i>=j)printf("%1d*%1d=%2d ", i, j, i*j);printf("\n");}getchar();return 0;}Exercise 6: Simple Functions1.Write a C program that reads several numbers and uses the functionround_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, thelarger 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 thenumber 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.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:");scanf("%f%f%f",&hour1,&hour2,&hour3);charge1=calculate_charges(hour1);charge2=calculate_charges(hour2);charge3=calculate_charges(hour3);printf("Car Hours Charge\n");printf("1%10.1f%10.2f\n",hour1,charge1);printf("2%10.1f%10.2f\n",hour2,charge2);printf("3%10.1f%10.2f\n",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-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 integer as input, andpasses 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) toreturn the value of:base exponentso 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. Thefunction 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:base exponentso 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. Thefunction should make no calls to any math library functions.(Hint: the recursive step will use the relationship:base exponent = base . base exponent - 1 and the base case will be when exponent is 1 since : base1 = base.)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 thenprints 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 of a number already read. Prepare for the “worst case” in which all 20 numbers#include<stdio.h>int main(){#define NUMROWS 2#define NUMCOLS 5int number[NUMROWS][NUMCOLS];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",&number[i][j]);}max1=number[0][0];min1=number[0][0];max2=number[1][0];min2=number[1][0];for(j=0;j<NUMCOLS;j++){ if(number[0][j]>=max1)max1=number[0][j];if(number[0][j]<=min1)min1=number[0][j];if(number[1][j]>=max2)max2=number[1][j];if(number[1][j]<=min2)min2=number[1][j];}printf("The max of the first row is %d\n",max1);printf("The min of the first row is %d\n",min1);printf("The max of the second row is %d\n",max2);printf("The min of the second row is %d\n",min2);printf("The max of two rows is ");if (max1>=max2)printf("%d\n",max1);else printf("%d\n",max2);printf("The min of two rows is ");if (min1<=min2)printf("%d\n",min1);else printf("%d\n",min2);return 0;}Exercise 9: More Arrays1.Write a program that enters 5 names of towns and their respective distance (an integer)from London in miles. The program will print of the names of the towns that are less than 100 miles from London. Use arrays and character strings to implement your program. 2. Write a program that prompts the user to type in four character strings, places these in anarray of strings, and then prints out: (e.g. I am Peter Pan)(i) The four strings in reverse order. (e.g. Pan Peter am I)(ii) The four strings in the original order, but with each string backwards. (e.g. I ma reteP naP)(iii) The four strings in reverse order with each string backwards. (e.g. naP reteP ma I)#include<stdio.h>int main(){char character[5][20];int integer[5];int i;for(i=0;i<5;i++){ printf("Please enter a name of a town:\n");scanf("%s",character[i]);printf("Enter its distance from London in miles:\n");scanf("%d",&integer[i]);}printf("The towns that are less than 100 miles from London are:\n");for(i=0;i<5;i++){if(integer[i]<100)printf("%s",character[i]);}return 0;Exercise 10: Pointers1. Write a program that reads 5 integers into an array, and then uses four different methods ofaccessing 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 eighth 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 andreturn simultaneously both the lowest and highest values in an array of type int. Suppose the size of the array is 6.ANSWER:#include<stdio.h>int main(){int num1[5],i;printf("Please input 5 numbers:\n");for (i=0;i<5;i++)scanf("%d",&num1[i]);//the first wayprintf("\nPrint them out in reverse order in the first way:\n");for (i=4;i>=0;i--)printf("%d",num1[i]);//the second wayprintf("\nPrint them out in reverse order in the second way:\n");int *num2;num2 = &num1[0];for (i=4;i>=0;i--)printf("%d",*(num2+i));//the third wayprintf("\nPrint them out in 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 = &num1[4];for (i=0;i<5;i++)printf("%d",*(num4-i));return 0;。

相关主题