C语言全部题目及答案

合集下载

C语言全部题目及答案

C语言全部题目及答案

C 语言全部题目及答案Exercise1: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 ase2-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 programusing the following methods:(a) Using four “printf” statemen ts.(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:#include<stdio.h>int main(){printf("1"); printf("2"); printf("3"); printf("4");return 0;}return 0; }#include<stdio.h>int main(){float days,minutes; days = 15; int main() {printf("This program!");return 0;int main(){float minutes,hours;printf("The numberminutes in 15 are %f\n", minutes);printf("This program!"); int main() {return 0; }#include<stdio.h> #include<stdio.h> #include<stdio.h>of days firstmyisExercise 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 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.25}#include<stdio.h>int main() {printf("%d%d%d%d",1,2,3,4);return 0; }hours = minutes / 60; printf("180 minutes equal to %f hours\n", hours);return 0; }#include<stdio.h>int main() {float minutes,hours;#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>{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 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 enter an integer number, then tells theuser whether it is an odd or even number. 3 . Write a program that reads in three integers and then determines and prints thelargest in the group.ANSWER: #include<stdio.h>#include<stdio.h>int main() {#include<stdio.h>int main() {int grade;printf("Please enter the grade:");scanf("%d",&grade);if (grade >= 60 && grade <= 100) printf("Passed."); else if (grade >= 0 && grade <60)#include<stdio.h>int main() {int a,b,c;printf("Please enter 3 integer numbers\n"); printf("Then I'll tell you which is thelargest\n"); 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)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 be followed by an appropriate suffix, ‘st’,‘nd’,‘rd’ or ‘th’. Use at least one switch statement.2 .Write a C program that uses a while loop to calculate and print the sum of the even integersfrom 2 to 30.3. A large chemical company pays its sales staff on a commission basis. They receive £ 200 perweek 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 their salary.Process one person’s figures at a time.Enter sales in pounds(-1to end):5000.00Salary is:650.00Enter sales in pounds(-1to end):00.00Salary is:200.00Enter sales in pounds(-1to end):1088.89Salary is:298.00Enter sales in pounds(-1to end):-1Optional:4. A mail order company sells five different products whose retail prices are shown in thefollowing table:Product Number1 2Retail Price (in pounds)2.984.503 4 59.98 4.49 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:#include<stdio.h>int main(){printf("Please enter three numbers for date:");int day,month,year;scanf("%d %d %d",&day,&month,&year);if(day>31)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){#include <stdio.h>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;}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 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/3 + 4/5 - 4/7 + 4/9 - 4/11 +....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;#include<stdio.h> int main() { 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 %.0f\n",b); }return 0;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 ifthe user enters ‘3’, then the program should calcula te 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= 1 2*1= 2 3*1= 3 ….9*1= 9ANSWER:#include<stdio.h> int main(){int a,b;a=0;b=1;do{a++;9*4=36 9*5=459*6=54#include <stdio.h>int main(){double c,pie,p;int a,b,d,n;printf("Enterterms:");scanf("%d",&a);printf("Pie=");9*7=63 9*8=729*9=81#include <stdio.h>int main(){int row,a,b,j;row=1;j=4;while(row<=5){for(a=j;a>=1;a=a-1) printf(" ");2*2= 43*2= 69*3=27 9*2=183*3= 9#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 ", i, j, i*j); printf("\n"); }getchar(); return 0;int main() { int a;for(a=2;a<=1024;a=a *2) printf("%d",a); return 0; }p=p+pie;if(n>1&&n<=a) {if(n%2!=0) printf("+"); else printf("-"); }printf("4/%d",d); n++; }printf("\n=%f",p); return 0; }}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++;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.Theprogram 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 firstpair,the larger of the second pair and the larger of the third e a function to return the larger of each pair.3.A car park charges a£2.00minimum fee to park for up to3hours,and anadditional£0.50for each hour or part hour in excess of three hours.The maximum charge for any given24-hour period is£10.00.Assume that no car parks for more than24hours at a time.Write a C program that will calculate and print the parking charges for each of3 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:Charge2.002.50 10.0014.50The program should use the function calculate_charges to determine the charge for each customer.ANSWER: #include<stdio.h> int main() {float larger_Number(float,float); float num1,num2,total=0;int a=0;while (a<3) { void round_to_nearest(float); float num; while (5){ total=total+larger_Number(num1,num2);a=a+1; }#include<stdio.h> #include<math.h> int main() {printf("Please input a number:"); scanf("%f",&num);TOTAL Hours 29.524.0 Car 4.0 1.5 2 3 1#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:"); 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);void round_to_nearest(float num1) {int 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:%d\n",near_integer); }printf("\nThe total is %f",total);return 0; }float larger_Number(float num1,float num2) { float larger;if (num1>=num2) {printf("%f",num1); larger=num1; } else {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,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, and0 if it is not.base exponentThe 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: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.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: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.The function should make no calls to any math library functions.(Hint:the recursive step will use the relationship:base exponent=base.base exponent-1and the base case will be when exponent is1since:base1=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);judge1(b);printf("Please evter a number");scanf("%d",&a);}return 0;}int judge(int x){if (x%2!=0)return (0);elsereturn (1);}void judge1(int x){if (x==1)printf("It's even\n"); elseprintf("It's odd\n");#include<stdio.h>int main(){int integerPower2(int,int);int base,exponent,result;printf("This program cancalculate the power\n");printf("Enter a integer basenumber:\n");scanf("%d",&base);printf("Enter a non-zerointeger number as the exponent:\n");scanf("%d",&exponent);result=integerPower2(base,expo nent);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 <stdio.h>int main(){int integerPower1(int,int);int base,exponent,answer;printf("Let us calculate the power\n");} printf("Please enter a integer base number:\n");scanf("%d",&base);printf("Please enter a non-zero integer number asthe 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;Exercise 08: Arrays1.Write a program that reads ten numbers supplied by the user into a singlesubscripted 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(2rows)(b) the whole array3 .Use a single-subscripted array to solve the following problem. Read in 20numbers,each of which is between10and100,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 are different. Use the smallest possible array to solve this problem.#include<stdio.h>int main(){#define MAXGRADES 10 int grades[MAXGRADES]; int i, total = 0;#include<stdio.h>int main(){#define MAXNUM 5int number[MAXNUM]; int i,j,a;ANSWER: #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];{ printf("Enter a number:"); scanf("%d",&grades[i]); }printf("\nThe average of the numbers\n"); for(i=0;i<MAXGRADES;i++) { printf("%d",grades[i]); total+=grades[i]; }average = total/10.0;for(i=0;i<MAXNUM;i++) scanf("%d",&number[i]);a=0;for(i=0;i<MAXNUM;i++) { for(j=0;j<i;j++) {if(number[i]==number[j])a++;}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 (aninteger) 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, placesthese in an array 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 differentmethods of accessing the members of an array to print them out in reverse order.//Pan Peter am I #include<stdio.h> int main() {char four[4][81]; int i;printf("Please input four words:\n");for(i=0;i<4;i++) scanf("%s",four[i]);printf("The four strings in reverse order is :\n"); for(i=3;i>=0;i--) { printf("%s",four[i]);//naP reteP ma I #include <stdio.h> int main() {char four[4][81]; int i,j;printf("Please input four words:\n"); for(i=0;i<4;i++)scanf("%s",four[i]); printf("The four strings are:\n"); for(i=3;i>=0;i--) {for(j=0;four[i][j]!='\0';j++); for(j=j-1;j>=0;j--) {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 (用一个函数)tofind and return 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;}#include<stdio.h>int main(){float num[8];int i;printf("Please input 8 floats:\n");for (i=0;i<8;i++)scanf("%f",&num[i]);//first, print out the second,fourth,sixth and eighthmembers of the arrayfor (i=1;i<8;i=i+2)printf("%f",*(num+i));//second, print out the sum of thefirst,third,fifthand seventhfloat total;for (i=0;i<8;i=i+2)total = total+*(num+i); #include<stdio.h>void SingleFunction(int *,int *); int main(){int num[6];int i;printf("Please input 6numbers:\n"); for (i=0;i<6;i++)scanf("%d",&num[i]);SingleFunction(num,num); return 0;}void SingleFunction(int *max,int *min) {int i,maxnum,minnum; maxnum = *max++;minnum = *min++;for(i=-1;i<5;i++)。

C语言经典编程100题(答案版)

C语言经典编程100题(答案版)

C语言经典程序100题(答案版)【程序1】题目:企业发放的奖金根据利润提成。

利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。

注意定义时需把奖金定义成长整型。

2.程序源代码:1.#include"stdio.h"2.#include"conio.h"3.main()4.{5.long int i;6.int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;7.scanf("%ld",&i);8.bonus1100000*0.1;9.bonus2bonus1+100000*0.75;10.bonus4bonus2+200000*0.5;11.bonus6bonus4+200000*0.3;12.bonus10bonus6+400000*0.15;13.if(i<100000)14.bonus i*0.1;15.else if(i<200000)16.bonus bonus1+(i-100000)*0.075;17.else if(i<400000)18.bonus bonus2+(i-200000)*0.05;19.else if(i<600000)20.bonus bonus4+(i-400000)*0.03;21.else if(i<1000000)22.bonus bonus6+(i-600000)*0.015;23.else24.bonus bonus10+(i-1000000)*0.01;25.printf("bonus%d",bonus);26.getch();27.}【程序2】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。

C语言基础练习100题(含答案)

C语言基础练习100题(含答案)

C语言基础练习100题(含答案)雷柳青编排1、下面程序的输出是________#include<stdio.h>void main(){ int k=11;printf("k=%d,k=%o,k=%x\n",k,k,k);}A) k=11,k=12,k=11 B) k=11,k=13,k=13 C) k=11,k=013,k=0xb D) k=11,k=13,k=b2、在下列选项中,不正确的赋值语句是________.A) ++t; B) n1=(n2=(n3=0));C) k=i=j; D) a=b+c=1;3、下面合法的C语言字符常量是__________.A) '\t' B) "A" C) 65 D) A4、字符(char)型数据在微机内存中的存储形式是____.A) 反码B) 补码C) EBCDIC码D) ASCII码5、设int a=12,则执行完语句a+=a-=a*a后,a的值是________A) 552 B) 264 C) 144 D) -2646、执行下面程序中的输出语句后,输出结果是______.#include<stdio.h>void main(){int a;printf("%d\n",(a=3*5,a*4,a+5));}A) 65 B) 20 C) 15 D) 107、下面程序的输出是__________.#include<stdio.h>void main(){int x=023;printf("%d\n",--x);}A) 17 B) 18 C) 23 D) 248、下面程序的输出是___________.#include<stdio.h>void main(){char ch1,ch2;ch1='A'+'5'-'3';ch2='A'+'6'-'3';printf("%d,%c\n",ch1,ch2);}A) 67,D B) B,C C) C,D D) 不确定的值9、以下程序的输出结果是________.#include<stdio.h>void main(){ int x=10,y=10;printf("%d %d\n",x--,--y);}A) 10 10 B) 9 9 C) 9 10 D) 10 910、若x和y都是int型变量,x=100,y=200,且有下面的程序片段:printf("%d",(x,y));上面程序片段的输出结果是_______.A) 200 B) 100C) 100 200 D) 输出格式符不够,输出不确定的值11、阅读下面的程序#include<stdio.h>void main(){int i,j;i=010;j=9;printf("%d,%d",i-j,i+j);}则程序的运行结果是________.A) 1,19 B) -1,19 C) 1,17 D) -1,1712、阅读下面的程序#include<stdio.h>void main(){int i,j,m,n;i=8;j=10;m=++i;n=j++;printf("%d,%d,%d,%d",i,j,m,n);}程序的运行结果是_________.A) 8,10,8,10 B) 9,11,8,10C) 9,11,9,10 D) 9,10,9,1113、若已定义int a,则表达式a=10,a+10,a++的值是___.A) 20 B) 10 C) 21 D) 1114、阅读下面的程序#include<stdio.h>void main(){int i,j;scanf("%3d%2d",&i,&j);printf("i=%d,j=%d\n",i,j);}如果从键盘上输入1234567<回车>,则程序的运行结果是________.A) i=123,j=4567 B) i=1234,j=567C) i=1,j=2 D) i=123,j=4515、下面程序的输出结果是________.#include<stdio.h>void main(){int a=-1, b=4, k;k=(++a<=0)&&(b--<=0);printf("%d,%d,%d\n",k,a,b);}A) 1,1,2 B) 1,0,3 C) 0,1,2 D) 0,0,316、下面程序的输出结果是_______.#include<stdio.h>void main(){int a=5,b=3;float x=3.14, y=6.5;printf("%d,%d\n",a+b!=a-b,x<=(y-=6.1));}A) 1,0 B) 0,1 C) 1,1 D) 0,017、若有以下定义和语句:int a=010, b=0x10, c=10;printf("%d,%d,%d\n",a,b,c);则输出结果是_________.A) 10,10,10 B) 8,16,10 C) 8,10,10 D) 8,8,1018、已知有double型变量x=2.5,y=4.7,整型变量a=7, 则表达式x+a%3*(int)(x+y)%2/4 的值是_________.A) 2.4 B) 2.5 C) 2.75 D) 019、设有以下语句:int x=10;x+=3+x%3,则x的值是._________A) 14 B) 15 C) 11 D) 12 20、若d为double型变量,则表达式d=1,d+5,d++的值是_______.A) 1 B) 6.0 C) 2.0 D) 1.021、若有定义int a=12,n=5,则表达式a%=(n%2)运算后,a的值__________.A) 0 B) 1 C) 12 D) 622、若有定义int x=3,y=2和float a=2.5,b=3.5,则表达式:(x+y)%2+(int)a/(int)b的值是____.A) 0 B) 2 C) 1.5 D) 123、在C语言中,以下叙述不正确的是________.A) 在C程序中,无论是整数还是实数,都能被准确无误的表示B) 在C程序中,变量名代表存储器中的一个位置C) 静态变量的生存期与整个程序的生存期相同D) C语言中变量必须先定义后引用24、设a为整型变量,不能正确表达数学关系10<a<15的C语言表达式是________.A) 10<a<15 B) a==11||a==12||a==13||a==14 C) a>10&&a<15 D) !(a<=10)&&!(a>=15)25、如果c为字符型变量,判断c是否为空格不能使用________.(假设已知空格ASCII码为32)A) if(c=='32') B) if(c==32)C) if(c=='\40') D) if(c==' ')26、执行下面程序时,若从键盘输入"2<CR>",则程序的运行结果是________.#include <stdio.h>void main( ){ int k; char cp;cp=getchar( );if (cp>='0' && cp<='9')k=cp-'0';else if (cp>='a' && cp<='f')k=cp-'a'+10;else k=cp-'A'+10;printf("%d\n",k);}A) 2 B) 4 C) 1 D) 1027、执行下面程序后,运行结果是________.#include <stdio.h>void main( ){ int x=41,y=1;if (x%3==0 && x%7==0){ y+=x;printf("y=%d\n",y);}else{y=x;printf("y=%d",y);}}A) y=41 B) y=43 C) y=42 D) y=128、运行下面程序时,从键盘输入"12,34,9<CR>",则输出结果是______.#include <stdio.h>void main( ){ int x,y,z;scanf("%d,%d,%d",&x,&y,&z);if (x<y)if (y<z)printf("%d\n",z);else printf("%d\n",y);else if (x<z)printf("%d\n",z);else printf("%d\n",x);}A) 34 B) 12 C) 9 D) 不确定的值29、运行下面程序时,从键盘输入字母H,则输出结果是________.#include <stdio.h>void main( ){ char ch;ch=getchar( );switch(ch){ case 'H':printf("Hello!\n");case 'G':printf("Good morning!\n");default:printf("Bye_Bye!\n");}}A) Hello! B) Hello!Good Morning!C) Hello! D) Hello!Good morning! Bye_Bye!Bye_Bye!30、执行下列程序段后的输出结果是_________.int x=1,y=1,z=1;x+=y+=z;printf("%d\n",x<y?y:x);A) 3 B) 2 C) 1 D) 431、设ch是char型变量,值为'A',则表达式ch=(ch>='A' && ch<='Z')?ch+32:ch的值是_____.A) Z B) a C) z D) A32、下面程序的输出结果是________.#include <stdio.h>void main( ){ int x=8,y=-7,z=9;if (x<y)if (y<0) z=0;else z-=1;printf("%d\n",z);}A) 8 B) 1 C) 9 D) 033、运行下面程序时,若从键盘输入"3,4 <CR>",则程序的输出结果是_______.#include <stdio.h>void main( ){ int a,b,s;scanf("%d,%d",&a,&b);s=a;if (s<b) s=b;s=s*s;printf("%d\n",s) ;}A) 14 B) 16 C) 18 D) 2034、下列程序的执行结果是_________.#include <stdio.h>void main( ){ int x=0,y=1,z=0;if (x=z=y)x=3;printf("%d,%d\n",x,z);}A) 3,0 B) 0,0 C) 0,1 D) 3,135、能够完成如下函数计算的程序段是______.┌-1 x<0y= ┤0 x=0└ 1 x>0A) y=1; B) if (x>=0)if(x!=0) if(x>0) y=1;if(x>0) y=1; else y=0;else y=0; else y=-1;C) y=0; D) y=-1;if (x>=0) if (x>0) y=1;if (x>0) y=1; else y=0;else y=-1;36、以下程序的执行结果是________.#include <stdio.h>void main( ){ int x=1,y=0;switch (x){case 1:switch (y){case 0:printf("first\n");break;case 1:printf("second\n");break;}case 2:printf("third\n");}}A) first B) firstsecond thirdC) first D) secondthird37、以下程序的执行结果是________.#include <stdio.h>void main( ){ int a,b,c,d,x;a=c=0;b=1;d=20;if (a) d=d-10;else if(!b)if (!c) x=15;else x=25;printf("d=%d\n",d);}A) d=20 B) d=10 C) d=15 D) 2538、下列程序执行后的输出结果是________.#include <stdio.h>void main( ){ int x,y=1,z;if ((z=y)<0) x=4;else if (y==0) x=5;else x=6;printf("%d,%d\n",x,y);}A) 4,1 B) 6,1 C) 5,0 D) 出错信息39、有如下程序#include <stdio.h>void main( ){ int x=1,a=0,b=0;switch(x){case 0: b++;case 1: a++;case 2: a++;b++;}printf("a=%d,b=%d\n",a,b);}该程序的输出结果是__________.A) a=2,b=1 B) a=1,b=1 C) a=1,b=0 D) a=2,b=240、下面程序的输出结果是_________.#include <stdio.h>void main( ){ int a=-1,b=1,k;if ((++a<0) && (b--<=0))printf("%d %d\n",a,b);elseprintf("%d %d\n",b,a);}A) -1 1 B) 0 1 C) 1 0 D) 0 041、假定w、x、y、z、m均为int型变量,有如下程序段:w=1;x=2;y=3;z=4;m=(w<x)?w:x; m=(m<y)?m:y; m=(m<z)?m:z;则该程序段执行后,m的值是_________.A) 4 B) 3 C) 2 D) 142、以下程序的输出结果是_________.main( ){ int a=100;if (a>100) printf("%d\n",a>100);else printf("%d\n",a<=100);}A) a<=100 B) 100 C) 0 D) 143、若执行下面的程序从键盘上输入9,则输出结果是.______________#include <stdio.h>void main( ){int n;scanf("%d",&n);if (n++<10) printf("%d\n",n);else printf("%d\n",n--);}A) 11 B) 10 C) 9 D) 844、以下程序段运行结果是________.int x=1,y=1,z=-1;x+=y+=z;printf("%d\n",x<y?y:x);A) 1 B) 2 C) 4 D) 不确定的值45、有以下程序#include <stdio.h>void main( ){ int a,b,c=246;a=c/100%9;b=(-1)&&(-1);printf("%d,%d\n",a,b);}输出结果是________.A) 2,1 B) 3,2 C) 4,3 D) 2,-146、运行下面程序时,若从键盘输入数据为"123",则输出结果是_______.#include "stdio.h"void main(){ int num,i,j,k,place;scanf("%d",&num);if (num>99)place=3;else if(num>9)place=2;elseplace=1;i=num/100;j=(num-i*100)/10;k=(num-i*100-j*10);switch (place){ case 3: printf("%d%d%d\n",k,j,i);break;case 2: printf("%d%d\n",k,j);break;case 1: printf("%d\n",k);}}A) 123 B) 1,2,3 C) 321 D) 3,2,147、执行下列程序后的输出结果是_______.#include <stdio.h>void main( ){ int k=4,a=3,b=2,c=1;printf("%d\n",k<a?k:c<b?c:a);}A) 4 B) 3 C) 2 D) 148、以下条件表达式中能完全等价于条件表达式if(x)中的x 的是____.A) (x==0) B) (x!=0) C) (x==1) D) (x!=1)49、若运行下面程序时,给变量a输入15,则输出结果是______.#include <stdio.h>void main( ){ int a,b;scanf("%d",&a);b=a>15?a+10:a-10;printf("%d\n",b) ;}A) 5 B) 25 C) 15 D) 1050、执行下面程序的输出结果是________.#include <stdio.h>void main( ){ int a=5,b=0,c=0;if (a=a+b) printf("****\n");else printf("####\n");}A) 有语法错误不能编译B) 能通过编译,但不能通过连接C) 输出****D) 输出####51、为了避免嵌套的if-else语句的二义性,C语言规定else总是与______组成配对关系.A) 缩排位置相同的ifB) 在其之前未配对的ifC) 在其之前尚未配对的最近的ifD) 同一行上的if52、以下程序段__________.x=-1;do{x=x*x;} while (!x);A)是死循环B)循环执行两次C)循环执行一次D)有语法错误53、对下面程序段描述正确的是_______.int x=0,s=0;while (!x!=0) s+=++x;printf("%d",s);A) 运行程序段后输出0B) 运行程序段后输出1C) 程序段中的控制表达式是非法的D) 程序段循环无数次54、下面程序段的输出结果是_______.x=3;do { y=x--;if (!y) {printf("*");continue;}printf("#");} while(x=2);A) ## B) ##* C) 死循环D)输出错误信息55、下面程序的运行结果是_______.#include<stdio.h>void main( ){ int a=1,b=10;do{ b-=a;a++;} while(b--<0);printf("%d,%d\n",a,b);}A) 3,11 B) 2,8 C) 1,-1 D) 4,956、下面程序段的运行结果是__________.int n=0;while (n++<=2)printf("%d",n);A) 012 B) 123 C) 234 D) 错误信息57、下面程序段的运行结果是________.int x=0,y=0;while (x<15) y++,x+=++y;printf("%d,%d",y,x);A) 20,7 B) 6,12 C) 20,8 D)8,2058、下面程序的运行结果是________.#include<stdio.h>void main(){ int s=0,i=1;while (s<=10){ s=s+i*i;i++;}printf("%d",--i);}A) 4 B) 3 C) 5 D) 659、下面程序段的运行结果是________.for(x=10;x>3;x--){ if(x%3) x--;--x; --x;printf("%d ",x);}A) 6 3 B) 7 4 C) 6 2 D) 7 3 60、下面程序的运行结果是________.#include<stdio.h>void main( ){ int a,b;a=-1;b=0;do {++a;++a;b+=a;} while(a<9);printf("%d\n",b);}A) 34 B) 24 C) 26 D) 2561、下面程序段的运行结果是___________.for(i=1;i<=5;)printf("%d",i);i++;A) 12345 B) 1234 C) 15 D) 无限循环62、下面程序的输出结果是__________.#include<stdio.h>void main( ){ int n=4;while (n--) printf("%d ",n--);}A) 2 0 B) 3 1 C) 3 2 1 D) 2 1 063、以下程序运行后的输出结果是________.#include<stdio.h>void main(){ int i=10,j=0;do{ j=j+1; i--;}while(i>2);printf("%d\n",j);}A) 50 B) 52 C) 51 D) 864、有如下程序#include<stdio.h>void main(){ int x=23;do{ printf("%d",x--);}while(!x);}该程序的执行结果是_______A) 321 B) 23C) 不输出任何内容D) 陷入死循环65、以下程序段的执行结果是_______.int i,j,m=0;for(i=1;i<=15;i+=4)for(j=3;j<=19;j+=4)m++;printf("%d\n",m);A) 12 B) 15 C) 20 D) 2566、下面程序的输出结果是___________.#include<stdio.h>void main( ){ int i;for(i=1;i<6;i++){ if (i%2!=0) {printf("#");continue;}printf("*");}printf("\n");}A) #*#*# B) ##### C) ***** D) *#*#*67、下面程序的输出结果是__________.#include<stdio.h>void main( ){ int x=10,y=10,i;for(i=0;x>8;y=++i)printf("%d %d ",x--,y);}A) 10 1 9 2 B) 9 8 7 6C) 10 9 9 0 D) 10 10 9 168、执行以下程序后,输出的结果是__________.#include<stdio.h>void main( ){ int y=10;do {y--;}while (--y);printf("%d\n",y--);}A) -1 B) 1C) 8D) 069、有如下程序#include<stdio.h>void main( ){ int n=9;while(n>6) {n--;printf("%d",n);}} 该程序段的输出结果是__________.A) 987 B) 876 C) 8765 D) 987670、有如下程序#include<stdio.h>void main( ){ int i,sum=0;for(i=1;i<=3;sum++) sum+=i;printf("%d\n",sum);}该程序的执行结果是___________.A) 6 B) 3 C) 死循环D) 071、以下循环体的执行次数是_______#include<stdio.h>void main( ){ int i,j;for(i=0,j=1; i<=j+1; i+=2, j--)printf("%d \n",i);}A) 3 B) 2 C) 1 D) 072、在执行以下程序时,如果从键盘上输入:ABCdef<回车>,则输出为________.#include <stdio.h>void main( ){ char ch;while ((ch=getchar( ))!='\n'){ if (ch>='A' && ch<='Z') ch=ch+32;else if (ch>='a' && ch<'z') ch=ch-32;printf("%c",ch);}printf("\n");}A) ABCdef B) abcDEF C) abc D) DEF73、下面程序的输出结果是__________.main( ){int i,k=0, a=0, b=0;for(i=1;i<=4;i++){k++;if (k%2==0) {a=a+k; continue;}b=b+k;a=a+k;}printf("k=%d a=%d b=%d\n",k,a,b);}A) k=5 a=10 b=4 B) k=3 a=6 b=4C) k=4 a=10 b=3 D) k=4 a=10 b=474、执行下面程序段后,k的值是_________.int i,j,k;for(i=0,j=10;i<j;i++,j--)k=i+j;A) 9 B) 11 C) 8 D) 1075、以下程序的功能是:从键盘上输入若干个学生的成绩, 统计并输出最高成绩和最低成绩,当输入负数时结束输入。

c语言试卷集(10套)-试题及答案

c语言试卷集(10套)-试题及答案

c语言试卷集(10套)-试题及答案C语言试卷集(10套)-试题及答案试卷一一、选择题(每题2分,共20分)1. C语言中,用于表示逻辑“真”的值是()。

A. 0B. 1C. -1D. 任意非零值答案:B2. 下列哪个选项是C语言中的合法变量名?()A. 2variableB. variable2C. variable_2D. variable!答案:C3. C语言中,用于定义一个整型数组的语句是()。

A. int array[10];B. int [10] array;C. int 10 array;D. int array=10;答案:A4. 下列哪个选项是C语言中的合法字符串常量?()A. "Hello"B. 'Hello'C. "HelloD. Hello答案:A5. C语言中,用于表示逻辑“假”的值是()。

A. 0B. 1C. -1D. 任意非零值答案:A6. 下列哪个选项是C语言中的合法浮点数常量?()A. 3.14B. 314.C. .314D. 3.14E答案:A7. C语言中,用于定义一个整型变量的语句是()。

A. int x;B. var x;C. x int;D. int = x;答案:A8. 下列哪个选项是C语言中的合法字符常量?()A. 'A'B. "A"C. 'AB'D. "AB"答案:A9. C语言中,用于定义一个字符数组的语句是()。

A. char array[10];B. char [10] array;C. char 10 array;D. char array=10;答案:A10. C语言中,用于表示字符常量的是()。

A. 'A'B. "A"C. AD. "A"答案:A二、填空题(每题3分,共15分)1. C语言中,用于定义一个整型变量并初始化为0的语句是()。

C语言所有题目以及答案

C语言所有题目以及答案

C语言所有题目以及答案一.判断题1.关系运算符<=和==具有相同的优先级。

氮气。

7&3+12的值为15 n3.在turboc中,整型数据在内存中占2个字节。

y4.C语言本身不提供输入输出语句,输入输出操作由函数实现。

y5。

Char[]=“verygood”:为字符串数组赋值是一种合法声明。

y6。

定义宏时,宏名称必须用大写字母表示。

n7。

如果inti=10,则j=2;然后执行I*=j+8;最后一个I的值是28n8。

语句scanf(“%7.2f”和&A);是一种合法的扫描功能。

n9。

C语言中%运算符的操作数必须是整数。

Y10.字符处理函数strcpy(str1,str2)的功能是把字符串1接到字符串2的后面。

n11.a=(b=4)+(c=6)是一个合法的赋值表达式。

y12.整数-32100可分配给int和Longint变量。

y13。

报表printf(“%F%%”,1.0/3);输出为0.333N14.若有宏定义:#defines(a,b)t=a;a=b;b=t由于变量t没有定义,所以此宏定义是错误的。

n15.x*=y+8等价于x=x*(y+8)y16.如果inti=10,则j=0;如果(J=0)I++,则执行该语句;我--;I的值是11 n17。

C语言只能逐个引用数组元素,而不能一次引用整个数组。

y18。

如果a=3、B=2、C=1,则关系表达式“(a>B)==C”的值为“真”。

y19。

C语言的所有函数都是外部函数。

Y20.如果想使一个数组中全部元素的值为0,可以写成inta[10]{0*10};n21.定义和声明(如有):inta;查克;漂浮物;scanf(“%d、%c、%f”、&a、&c、&f);如果你通过键盘输入:10,a,12.5,那么a=10,C=a',f=12.5y22.如果有一个字符串,其中第十个字符为‘\\n’,则此字符串的有效字符为9个。

c语言计算机试题及答案

c语言计算机试题及答案

c语言计算机试题及答案1. 单项选择题(1) 下列哪个选项是C语言中合法的变量名?A. 2variableB. variable2C. variable-2D. variable_2答案:D(2) C语言中,以下哪个关键字用于定义函数?A. classB. functionC. defD. void答案:D(3) 在C语言中,用于定义字符串的字符数组的声明方式是?A. int str[10];B. char str[10] = "Hello";C. string str = "Hello";D. char str[] = "Hello";答案:B2. 多项选择题(1) 下列哪些选项是C语言中合法的整数常量?A. 012B. 0x1AC. 10LD. 1.0答案:A, B, C(2) 在C语言中,以下哪些运算符用于比较操作?A. ==B. !=C. <=D. %答案:A, B, C3. 填空题(1) 在C语言中,用于定义一个整型数组的语句是____。

答案:int array_name[size];(2) C语言中,用于循环结构的关键字是____。

答案:for, while, do-while4. 简答题(1) 请简述C语言中指针的概念。

答案:指针是一个变量,其值为另一个变量的地址。

通过指针,程序可以直接访问和操作内存地址。

(2) 解释C语言中结构体的作用。

答案:结构体是一种用户自定义的数据类型,允许将不同的数据类型组合成一个单一的数据结构,以便于数据的组织和管理。

5. 编程题(1) 编写一个C语言程序,实现将一个整数数组逆序输出。

答案:```c#include <stdio.h>int main() {int arr[] = {1, 2, 3, 4, 5};int n = sizeof(arr) / sizeof(arr[0]); for(int i = n - 1; i >= 0; i--) {printf("%d ", arr[i]);}return 0;}```。

c语言题目及答案

c语言题目及答案

注意:所有答案都要写在答题卡上一、单项选择题(20分,每题2分)1.执行下列程序段后,正确的结果是(B)int k, a[3][3] = {1,2,3,4,5,6,7,8,9};for(k=0; k<3; k++)printf(“%2d”, a[k][k]);A) 1 2 3 B) 1 5 9 C) 1 4 7 D) 3 6 92.若a是int类型变量,则计算表达式a=25/3%3的值是:(B)A)3 B)2 C)1 D)03.下面正确的字符常量是:(C)A)“c” B)‘\\’’ C)‘W’ D)‘’4.C语言中,运算对象必须是整型的运算符是:(B)A)% B)/ C)* D)+5.数字字符0的ASCII值为48,若有以下程序main(){char a='1', b='2';printf("%c,",b++);printf("%d\n",b-a);}程序运行后的输出结果是。

(B)A)3,2 B)50,2 C)2,2 D)2,50 6.以下语句或语句组中,能正确进行字符串赋值的是。

(D)A)char *sp;*sp="right!"; B)char s[10];s="right!";C)char s[10];*s="right!"; D)char *sp="right!";7.for(i=0;i<10;i++)if(i〈=5〉break;则循环结束后i的值为(B)A)0 B)1 C)5 D)108.执行下列程序段后,正确的结果是(C)char x[8] = {8,7,6,5,0,0}, *s;s = x+1;printf(“%d\n”, s[2]);A) n B) 0 C) 5 D) 69.C语言规定,简单变量作为实参时,他和对应形参之间的数据传递方式是:AA)单向值传递B) 地址传递C) 相互传递D) 由用户指定方式10.设有数组定义:char str[]=”China”;则下面的输出为(C)printf(“%d”,strlen(str));A)4 B)5 C)6 D)7二、填空题(30分,每空2分)1.下列程序段的输出结果是 3 。

C语言考试题及答案

C语言考试题及答案

一、单项选择题:(10分,每题2分)1.char *p[10];该语句声明了一个:。

A)指向含有10个元素的一维字符型数组的指针变量pB)指向长度不超过10的字符串的指针变量pC)有10个元素的指针数组p,每个元素可以指向一个字符串D) 有10个元素的指针数组p,每个元素存放一个字符串2.若int x;且有下面的程序片断,则输出结果为:。

for (x=3; x<6; x++){printf((x%2) ? "##%d" : "**%d\n", x);}A) ##3B) **3C) **3D)##3**4**4 ##4 ##4**5 ##5##5 **53.在while(!x)语句中的!x与下面条件表达式等价的是:。

A) x!=0 B) x==1 C) x!=1 D) x==04.已知struct point{int x;int y;};struct rect{struct point pt1;struct point pt2;};struct rect rt;struct rect *rp = &rt;则下面哪一种引用是不正确的________。

A) B) (*rp). C) rp-> D)rt->5.若二维数组a有m行n列,则下面能够正确引用元素a[i][j]的为:。

A) *(a+j*n+i) B) *(a+i*n+j) C) *(*(a+i)+j) D) *(*a+i)+jCDDDC二、分析程序并写出运行结果。

(25分,每题5分)1.#include <>main(){int n;static char *monthName[]={"Illegal month", "January", "February","March", "April", "May", "June", "July", "August","September", "October", "November", "December"};for (n=1; n<=12; n++){printf("%s\n", monthName[n]);}}运行结果是:JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember2.#include<>#define ARR_SIZE 7void YH(int a[][ARR_SIZE], int n);void PrintYH(int a[][ARR_SIZE], int n);main(){int a[ARR_SIZE][ARR_SIZE];YH(a, ARR_SIZE-1);PrintYH(a, ARR_SIZE-1);}void YH(int a[][ARR_SIZE], int n){int i, j;for (i=1; i<=n; i++){for (j=1; j<=i; j++){if (j==1 || i==j){a[i][j] = 1;}else{a[i][j] = a[i-1][j-1] + a[i-1][j];}}}}void PrintYH(int a[][ARR_SIZE], int n){int i , j ;for (i=1; i<=n; i++){for (j=1; j<=i; j++){printf("%4d", a[i][j]);}printf("\n");}}运行结果是:11 11 2 11 3 3 11 4 6 4 11 5 10 10 5 13.#include <>main(){int i, n;for (i=1; i<=5; i++){printf("Please enter n:");scanf("%d", &n);if (n <= 0) continue;printf("n = %d\n", n);}printf("Program is over!\n");}程序运行时输入:1 -2 3 -4 5↙运行结果是:n = 1Please enter n: Please enter n:n = 3 Please enter n: Please enter n:n = 5 Program is over!4.#include <>void Func(int n);main(){int i;for (i = 0; i < 2; i++){Func(i);}}void Func(int n){static int t = 1;printf("t=%d\n", t++);}运行结果是:t=1t=25.#include <>int Func(int i);main(){int i;for (i=3; i<5; i++){printf(" %d", Func(i));}printf("\n");}int Func(int i){static int k = 10;for (; i>0; i--){k++;}return (k);}运行结果是:13 17三、阅读并完成程序,在标有下划线的空白处填入适当的表达式或语句,使程序完整并符合题目要求。

C语言全部题目及答案

C语言全部题目及答案
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 oneswitchstatement.
2.Write a programthat reads in the radius of a circleandprints the circle’s diameter, circumference and area. Use the value 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:
return 0;
}
#include<>
int main()
{
printf("1");

(完整版)C语言考试题库及答案

(完整版)C语言考试题库及答案

(完整版)C语言考试题库及答案一、选择题1. 以下哪个选项是C语言的合法标识符?A. intB. 2abcC. voidD. a+b答案:C2. 在C语言中,下列哪个选项是正确的数据类型?A. floatB. integerC. doubleD. All of the above答案:D3. 若变量定义如下:int a = 5;,则执行语句printf("%d", a++); 后,a的值是多少?A. 4B. 5C. 6D. 无法确定答案:C4. 以下哪个函数用于动态分配内存?A. malloc()B. alloc()C. new()D. calloc()答案:A5. 在C语言中,哪个运算符用于取模(取余)?A. %B. /C. &D. |答案:A以下是判断题部分:6. C语言中的数组名可以作为指向该数组首元素的指针使用。

()答案:正确7. 在C语言中,结构体变量可以作为函数的参数传递。

()答案:正确8. 在C语言中,静态存储类别的变量在程序运行期间始终占据内存空间。

()答案:正确二、填空题9. 在C语言中,定义一个整型变量需要使用关键字______。

答案:int10. 若变量定义如下:int a = 10;,则执行语句printf("%d", a--); 后,a的值是______。

答案:911. 在C语言中,用于动态分配内存的函数是______。

答案:malloc12. 在C语言中,用于释放动态分配的内存的函数是______。

答案:free13. 在C语言中,用于清空标准输入缓冲区的函数是______。

答案:getchar()三、编程题14. 编写一个C语言程序,实现以下功能:输入一个整数,输出它的阶乘。

答案:```c#include <stdio.h>int main() {int n, i;long factorial = 1;printf("Enter an integer: ");scanf("%d", &n);if (n < 0) {printf("Factorial of a negative number doesn't exist.\n");} else {for (i = 1; i <= n; i++) {factorial = i;}printf("Factorial of %d is %ld\n", n, factorial);}return 0;}```15. 编写一个C语言程序,实现以下功能:输入一个字符串,判断它是否是回文(正读和反读都一样的字符串)。

c语言考试题及答案以及解析

c语言考试题及答案以及解析

c语言考试题及答案以及解析一、选择题1. 下列哪个选项是C语言中的关键字?A. intB. floatC. doubleD. string答案:A解析:在C语言中,关键字是保留的词汇,用于表示特定的语法结构。

int是C语言中用于声明整型变量的关键字。

2. C语言中,哪个运算符用于计算两个数的乘积?A. +B. -C. *D. /答案:C解析:在C语言中,乘法运算符是星号(*),用于计算两个数的乘积。

二、填空题1. 在C语言中,以下代码片段的输出结果是:```cint main() {int a = 5;printf("%d", a);return 0;}```输出结果为:_________答案:5解析:代码中的printf函数用于输出变量a的值,a被初始化为5,因此输出结果为5。

2. 下列代码段中,变量b的值是多少?```cint main() {int a = 10, b;b = a + 5;printf("%d", b);return 0;}```变量b的值为:_________答案:15解析:变量b被赋值为变量a加上5,由于a的值为10,所以b的值为15。

三、简答题1. 简述C语言中的数组是什么,并给出一个数组的声明和初始化的例子。

答案:C语言中的数组是一种数据结构,用于存储相同类型的多个元素。

数组的声明需要指定元素的类型和数组的长度。

数组的初始化可以在声明时完成。

解析:例如,声明一个整型数组并初始化:```cint numbers[5] = {1, 2, 3, 4, 5};```2. 解释C语言中的函数是什么,并给出一个简单函数的定义和调用的例子。

答案:C语言中的函数是一段具有特定功能的代码块,可以接收参数,并返回一个值。

函数的定义包括函数的返回类型、名称、参数列表和函数体。

函数的调用是使用函数名称和必要的参数来执行函数。

解析:例如,定义一个函数来计算两个数的和,并调用它:```cint add(int x, int y) {return x + y;}int main() {int result = add(5, 10);printf("%d", result);return 0;}```四、编程题1. 编写一个C语言程序,实现输入两个整数,输出它们的和。

C语言基础练习100题含标准答案

C语言基础练习100题含标准答案

C语言基础练习100题含标准答案1. 编写一个C语言程序,要求输出"Hello, World!"#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}2. 编写一个C语言程序,要求输出两个整数的和。

#include <stdio.h>int main() {int a, b, sum;printf("请输入两个整数:");scanf("%d %d", &a, &b);sum = a + b;printf("两个整数的和为:%d\n", sum);return 0;}3. 编写一个C语言程序,要求输出一个整数的平方。

#include <stdio.h>int main() {int num, square;printf("请输入一个整数:");scanf("%d", &num);square = num * num;printf("该整数的平方为:%d\n", square);return 0;}4. 编写一个C语言程序,要求输出1到10的所有整数。

#include <stdio.h>int main() {int i;for (i = 1; i <= 10; i++) {printf("%d ", i);}printf("\n");return 0;}5. 编写一个C语言程序,要求输出1到10的所有整数的和。

#include <stdio.h>int main() {int i, sum = 0;for (i = 1; i <= 10; i++) {sum += i;}printf("1到10的所有整数的和为:%d\n", sum);return 0;}6. 编写一个C语言程序,要求输入一个字符串并输出该字符串。

c语言分章节试题及答案

c语言分章节试题及答案

c语言分章节试题及答案1. 变量声明- 题目:在C语言中,以下哪个选项是正确的变量声明方式?A. int a = 0;B. int a = 0.0;C. int a = "hello";D. int a = 0, b = 0;- 答案:A和D。

2. 数据类型- 题目:以下哪个选项不是C语言中的基本数据类型?A. intB. floatC. charD. string- 答案:D。

3. 运算符优先级- 题目:在C语言中,以下哪个运算符的优先级最高?A. 乘法B. 加法C. 赋值D. 条件运算符- 答案:A。

4. 函数定义- 题目:以下哪个是正确的C语言函数定义?A. int myFunction(int a, int b) { return a + b; }B. int myFunction(int a, int b) { return a - b; }C. int myFunction(int a, int b) { return a * b; }D. All of the above- 答案:D。

5. 数组初始化- 题目:以下哪个是正确的C语言数组初始化方式?A. int arr[5] = {1, 2, 3, 4, 5};B. int arr[5] = {1, 2, 3};C. int arr[5] = {1, 2, 3, 4};D. int arr[5] = {1, 2, 3, 4, 5, 6};- 答案:A。

6. 指针- 题目:以下哪个选项正确地声明了一个指向整数的指针?A. int *ptr;B. int ptr[];C. int *ptr = 0;D. int ptr = 0;- 答案:A和C。

7. 字符串操作- 题目:以下哪个函数用于比较两个字符串是否相等?A. strcat()B. strcmp()C. strcpy()D. strlen()- 答案:B。

8. 条件语句- 题目:以下哪个是C语言中正确的if语句?A. if (x > 10)x = 20;B. if (x > 10)x = 20C. if (x > 10)x = 20;else x = 10;D. All of the above- 答案:A。

C语言题库(含答案)

C语言题库(含答案)

C语言题库(含答案)一、简答题。

( 共14题 ,共0分,每题0分 )1. int i=10, j=10, k=3; k*=i+j; k最后的值是?答:k = 60ok2. 写出程序结果:void Func(char str[100]){printf("%d\n", sizeof(str));}答:4或者8(如果操作系统为32位则为4,64位则为8)是地址吗3. 写出sizeof(struct name2)的结果struct name2{char str;int num;short x;}不会!看结构答:此题考察结构对齐的知识,结果为124. 写出sizeof(struct name1) 的结果struct name1{char str;short x;int num;}不会!答:同样考察的是结构对齐的知识,结果为85. A.c 和B.c两个c文件中使用了两个相同名字的static变量,编译的时候会不会有问题?这两个static变量会保存到哪里(栈还是堆或者其他的)?答:没有问题,static变量只在当前文件中有效,也就是说static 变量的作用域属于所在的文件域。

static变量保存在全局/静态区6. (void *)ptr 和(*(void**))ptr的结果是否相同?答:相同。

首先第一个(void *)ptr将ptr转换为空指针类型(一级指针),(*(void**))ptr相当于先将ptr转换为空类型指针(二级指针)。

二级指针是指向指针的指针,在前面加上*(解指针),也就是空类型指针了(一级指针)。

7. #define DOUBLE(x) x+x ,i = 5*DOUBLE(5);i 是多少?答: i = 5 * 5+5 = 30;看书上的结构理解下意思8. 下面的声明都是什么意思?const int a;int const a;const int *a;int * const a;int const * const a ;答:第一个定义常量a,第二个也是定义常量a,第三个定义常量指针a,第四个定义指针常量a,第五个定义指向常量的指针常量(相当于const引用)。

(完整版)C语言题库(带详解答案)

(完整版)C语言题库(带详解答案)

一单项选择题1.(A)是构成C语言程序的基本单位。

A、函数B、过程C、子程序D、子例程2.C语言程序从 C开始执行。

A) 程序中第一条可执行语句 B) 程序中第一个函数C) 程序中的main函数 D) 包含文件中的第一个函数3、以下说法中正确的是(C)。

A、C语言程序总是从第一个定义的函数开始执行B、在C语言程序中,要调用的函数必须在main( )函数中定义C、C语言程序总是从main( )函数开始执行D、C语言程序中的main( )函数必须放在程序的开始部分4.下列关于C语言的说法错误的是(B)。

A) C程序的工作过程是编辑、编译、连接、运行B) C语言不区分大小写。

C) C程序的三种基本结构是顺序、选择、循环D) C程序从main函数开始执行5.下列正确的标识符是(C)。

A.-a1B.a[i]C.a2_iD.int t5~8题为相同类型题考点:标识符的命名规则(1)只能由字母、数字、下划线构成(2)数字不能作为标识符的开头(3)关键字不能作为标识符选项A中的“-” ,选项B中“[”与“]”不满足(1);选项D中的int为关键字,不满足(3)6.下列C语言用户标识符中合法的是( B)。

A)3ax B)x C)case D)-e2 E)union选项A中的标识符以数字开头不满足(2);选项C,E均为为关键字,不满足(3);选项D中的“-”不满足(1);7.下列四组选项中,正确的C语言标识符是(C)。

A) %x B) a+b C) a123 D) 123选项A中的“%” ,选项B中“+”不满足(1);选项D中的标识符以数字开头不满足(2)8、下列四组字符串中都可以用作C语言程序中的标识符的是(A)。

A、print _3d db8 aBcB、I\am one_half start$it 3paiC、str_1 Cpp pow whileD、Pxq My->book line# His.age选项B中的“\”,”$” ,选项D中“>”,”#”,”.”,”-”不满足(1);选项C中的while 为关键字,不满足(3)9.C语言中的简单数据类型包括(D)。

c语言考试试题及答案

c语言考试试题及答案

c语言考试试题及答案一、选择题1. 在C语言中,下列哪个关键字用于声明一个变量的名称?A. defineB. intC. printfD. scanf答案:B2. 函数的返回类型可以是下列哪种类型?A. intB. floatC. charD. all of the above答案:D3. 下列哪个运算符用于访问结构体变量的成员?A. .B. ->C. :D. ,答案:A4. 文件指针是下列哪种类型?A. FILEB. pointerC. file*D. fp答案:A5. 下面哪个循环结构在执行前先判断条件,再决定是否执行循环体?A. forB. whileC. do-whileD. switch答案:B二、填空题1. C语言中,用于定义常量的关键字是____。

答案:const2. 在C语言中,用于输出结果到控制台的函数是____。

答案:printf3. C语言中,用于读取用户输入的函数是____。

答案:scanf4. 下面是一个二维数组的声明方式:int arr[3][4];那么该数组的元素个数是____。

答案:125. C语言中,用于在循环执行过程中跳过当前迭代的关键字是____。

答案:continue三、编程题请编写一个程序,实现从键盘输入两个整数,然后输出它们的和。

```c#include <stdio.h>int main(){int num1, num2, sum;printf("请输入第一个整数:");scanf("%d", &num1);printf("请输入第二个整数:");scanf("%d", &num2);sum = num1 + num2;printf("两个整数的和为:%d\n", sum);return 0;}```以上是一道简单的示例题目,通过该题目可以熟悉C语言的基本输入输出、变量声明和运算符等基础知识。

(完整版)C语言试题及答案解析

(完整版)C语言试题及答案解析

C语言一、选择题(第题2分,共20分)1.一个C程序的执行是从 A 。

A) 本程序的main函数开始,到main函数结束B) 本程序文件的第一个函数开始,到本程序文件的最后一个函数结束C) 本程序文件的第一个函数开始,到本程序main函数结束D) 本程序的main函数开始,到本程序文件的最后一个函数结束2.若x、i、j、k都是int型变量,则计算下面表达式后,x的值为 C 。

x=(i=4,j=16,k=32)A) 4 B) 16 C) 32 D) 523.设C语言中,一个int型数据在内存中占2个字节,则unsigned int 型数据的取值范围为 C 。

A) 0~255 B) 0~32767 C) 0~65535 D) 0~21474836474.设有说明:char w; int x; float y; double z;则表达式w*x+z-y值的数据类型为 D 。

A) float B) char C) int D) double5. putchar函数可以向终端输出一个D 。

A) 整型变量表达式 B) 实型变量值C) 字符串 D) 字符或字符型变量值6. printf函数中用到格式符%5s,其中数字5表示输出的字符串占用5列。

如果字符串长度大于5,则输出按方式 B ;如果字符串长度小于5,则输出按方式 C 。

A) 从左起输出该字符串,右补空格 B) 按原字符长从左向右全部输出C) 右对齐输出该字符串,左补空格 D) 输出错误信息7.判断char型变量ch是否为大写字母的正确表达式是 C 。

A) ‘A’<=ch<=‘Z’ B) (ch>=‘A’)&(ch<=‘Z’)C) (ch>=‘A’)&&(ch<=‘Z’) D) (‘A’<= ch)AND(‘Z’>= ch)8.已知int x=10,y=20,z=30;以下语句执行后x,y,z的值是 B 。

c语言习题及参考答案

c语言习题及参考答案

第1章C语言概述(一)选择题1.一个C程序的执行是从 A 。

答案AA) 本程序的main函数开始,到main函数结束B) 本程序文件的第一个函数开始,到本程序文件的最后一个函数结束C) 本程序文件的第一个函数开始,到本程序main函数结束D) 本程序的main函数开始,到本程序文件的最后一个函数结束2.以下叙述不正确的是 D 。

A) 一个C源程序必须包含一个main函数B) 一个C源程序可由一个或多个函数组成C) C程序的基本组成单位是函数D) 在C程序中,注释说明只能位于一条语句的后面3.以下叙述正确的是 C 。

A) 在对一个C程序进行编译的过程中,可发现注释中的拼写错误B) 在C程序中,main函数必须位于程序的最前面C) C语言本身没有输入输出语句D) C程序的每行中只能写一条语句4.一个C语言程序是由 B 。

A)一个主程序和若干个子程序组成B) 函数组成C) 若干过程组成D) 若干子程序组成5. C语言程序编译时,程序中的注释部分______D______.A) 参加编译,并会出现在目标程序中B) 参加编译,但不会出现在目标程序中C) 不参加编译,但会出现在目标程序中D) 不参加编译,也不会出现在目标程序中(二)填空题1、C语句结束符为____;_____________.2、C语句注释符号为:_/* */______________.3、一个C程序总是从____main函数______________开始执行的。

第3章数据类型、运算符与表达式(一)选择题1. 若x、i、j、k都是int型变量,则计算下面表达式后,x的值为( C )。

x=(i=4,j=16,k=32)A) 4 B) 16 C) 32 D) 522.下列四组选项中,均不是C语言关键字的选项是( A )。

A) define IF typeB) getc char printfC) include case scanfD) while go pow3. 下列四组选项中,均是不合法的用户标识符的选项是( B )。

c语言试题目及答案

c语言试题目及答案

c语言试题目及答案1. 选择题1) 在C语言中,下面哪个选项是无效的标识符?A. myVarB. _varC. 3varD. var3正确答案:C. 3var2) 下面哪行代码可以正确输出字符串 "Hello, World!"?A. printf("Hello, World!");B. print("Hello, World!");C. console.log("Hello, World!");D. cout << "Hello, World!";正确答案:A. printf("Hello, World!");3) 在C语言中,下面哪个数据类型可以用来存储小数值?A. intB. charC. floatD. bool正确答案:C. float2. 填空题1) 下划线是在C语言中用作什么目的?答案:下划线是用来连接多个单词以创建一个标识符。

2) 在C语言中,用于存储单个字符的数据类型是__char__。

3) C语言中,用于声明变量的关键字是__int__。

3. 程序分析题给定以下C语言程序,请写出代码中缺失的部分:```c#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("两数之和为:%d", sum);return 0;}```程序分析:上述代码是一个简单的C语言程序,用于接收用户输入的两个整数,并计算它们的和。

缺失部分为```printf("两数之和为:%d", sum);```中的换行符。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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;。

相关文档
最新文档