C语言编程练习.pptx

上传人:莉*** 文档编号:87416535 上传时间:2023-04-16 格式:PPTX 页数:104 大小:390.86KB
返回 下载 相关 举报
C语言编程练习.pptx_第1页
第1页 / 共104页
C语言编程练习.pptx_第2页
第2页 / 共104页
点击查看更多>>
资源描述

《C语言编程练习.pptx》由会员分享,可在线阅读,更多相关《C语言编程练习.pptx(104页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、4/13/2023Chapter 4 Managing Input and Output Operations 4.2main()float x,y;printf(Please input the value of x and y:n);printf(x=);scanf(%f,&x);printf(y=);scanf(%f,&y);printf(a)(x+y)/(x-y)=%fn,(x+y)/(x-y);printf(b)(x+y)/2=%fn,(x+y)/2);printf(c)(x+y)(x-y)=%fn,(x+y)*(x-y);第1页/共104页4/13/2023Chapter 4 Ma

2、naging Input and Output Operations 4.34.3 Write a program to read the following numbers,round them off to the nearest integers and print out the results in integer form:35.750.21-23.73-46.45第2页/共104页4/13/2023Chapter 4 Managing Input and Output Operations 4.3main()float x;printf(Please input the numb

3、er:);scanf(%f,&x);printf(The number is:%.0fn,x);第3页/共104页4/13/2023Chapter 4 Managing Input and Output Operations 4.54.5 Write an interactive program to demonstrate the process of multiplication.The program should ask the user to enter two two-digit integers and print the product of integers as shown

4、 below.45 37745 is 315345 is 135Add them1665 第4页/共104页4/13/2023Chapter 4 Managing Input and Output Operations 4.5main()int x,y,m,n;printf(Please input 2 two-digit integers:);scanf(%d%d,&x,&y);printf(ntt%6dntt*%5dn,x,y);printf(tt_n);printf(%d*%d istt%6dn,y%10,x,y%10*x);printf(%d*%d istt%5dn,y/10,x,y/

5、10*x);printf(tt_n);printf(Add themt%6dn,y*x);printf(tt_n);第5页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.15.1 Write a program to determine whether a given number is odd of even and print the message NUMBER IS EVEN or NUMBER IS ODD(a)without using else option,and(b)with else option.第6页/共

6、104页4/13/2023Chapter 5 Decision Making and Branching 5.1main()/*(a)*/int n;printf(Please input an integer:);scanf(%d,&n);if(n%2)printf(%d IS ODDn,n);if(n%2=0)printf(%d IS EVENn,n);第7页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.1main()/*(b)*/int n;printf(Please input an integer:);scanf(%

7、d,&n);if(n%2)printf(%d IS ODDn,n);elseprintf(%d IS EVENn,n);第8页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.35.3 A set of two liner equations with two unknown x1 and x2 is given below:ax1+bx2=mcx1+dx2=nThe set has a unique solution provided the denominator ad-cb is not equal to zero.Writ

8、e a program that will read the values of constants a,b,c,d,m and n and compute the values of x1 and x2.An appropriate message should be printed if ad-cb=0.第9页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.3main()float a,b,c,d,m,n;printf(About a set of two liner equations:n);printf(tax1+bx2

9、=mntcx1+dx2=nn);printf(Please input the value of a,b,c,d,m and n:n);printf(a=);scanf(%f,&a);printf(b=);scanf(%f,&b);printf(c=);scanf(%f,&c);printf(d=);scanf(%f,&d);printf(m=);scanf(%f,&m);printf(n=);scanf(%f,&n);printf(n About the set of two liner equations:n);printf(t%.2fx1+%.2fx2=%.2fnt%.2fx1+%.2f

10、x2=%.2fn,a,b,m,c,d,n);if(a*d-c*b=0)printf(Error:the denominator is zero!n);else printf(The result:x1=%.2f,x2=%.2fn,(m*d-b*n)/(a*d-c*b),(n*a-m*c)/(a*d-c*b);第10页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.55.5 Admission to a professional course is subject to the following conditions:(a)Ma

11、rks in Mathematics=60(b)Marks in Physics=50(c)Marks in Chemistry=40(d)Total in all three subjects=200orTotal in Mathematics and Physics=150Given the marks in the three subjects,write a program to process the applications to list the eligible candidates.第11页/共104页4/13/2023Chapter 5 Decision Making an

12、d Branching 5.5main()float math,phy,chem;printf(Please input scores of the 3 subject:);printf(Mathematics:);scanf(%f,&math);printf(Physics:);scanf(%f,&phy);printf(Chemistry:);scanf(%f,&chem);if(math=60&phy=50&chem=40&(math+phy+chem=200|math+phy=150)printf(Admitted!n);else printf(Not admittedn);第12页/

13、共104页4/13/2023Chapter 5 Decision Making and Branching 5.85.8 A cloth showroom has announced the following seasonal discounts on purchase of items:Write a program using switch and if statements to compute the net amount to be paid by a customer.Purchase amountDiscountMill clothHandloom items0100-5%10

14、12005%7.5%2013007.5%10.0%Above 30010.0%15.0%第13页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.8main()float price,discount;char category;printf(Category(m/h):);scanf(%c,&category);printf(Price:);scanf(%f,&price);if(category=m)switch(int)(price-1)/100 )case 0:discount=0;break;case 1:discoun

15、t=0.05;break;case 2:discount=0.075;break;default:discount=0.1;else switch(int)(price-1)/100 )case 0:discount=0.05;break;case 1:discount=0.075;break;case 2:discount=0.1;break;default:discount=0.15;printf(The net amount to be paid is%.2fn,price*(1-discount);第14页/共104页4/13/2023Chapter 5 Decision Making

16、 and Branching 5.95.9 Write a program that will read the value of x and evaluate the following using(a)nested if statements(b)else if statements,and (c)conditional operator?:第15页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.9main()/*(a)*/float x;printf(Please input the value of x:);scanf(

17、%f,&x);if(x!=0)if(x 0)printf(y=1n);elseprintf(y=-1n);elseprintf(y=0n);第16页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.9main()/*(b)*/float x;printf(Please input the value of x:);scanf(%f,&x);if(x 0)printf(y=1n);elseif(x=0)printf(y=0n);elseprintf(y=-1n);第17页/共104页4/13/2023Chapter 5 Decisi

18、on Making and Branching 5.9main()/*(c)*/float x;printf(Please input the value of x:);scanf(%f,&x);printf(y=%dn,x 0?1:x 0?-1:0);第18页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.105.10 Write a program to compute the real roots of a quadratic equationThe roots are given by the equationsThe

19、program should request for the values of the constants a,b and c and print the values of x1,and x2.Use the following rules:(a)No solution,if both a and b are zero(b)There is only one root,if a=0(x=-c/b)(c)There are no real roots,if b2-4ac is negative(d)Otherwise,there are two real rootsTest your pro

20、gram with appropriate data so that all logical paths are working as per your design.Incorporate appropriate output messages.第19页/共104页4/13/2023Chapter 5 Decision Making and Branching 5.10#include main()float a,b,c,f;printf(Please input the value of a,b and c in the quadratic equation:n ax*x+bx+c=0n)

21、;printf(a=);scanf(%f,&a);printf(b=);scanf(%f,&b);printf(c=);scanf(%f,&c);printf(About%.2fx*x+%.2fx+%.2f=0:,a,b,c);if(a=0&b=0)printf(There is no solution!n);else if(a=0)printf(There is only one root:%.2f.n,-c/b);elsef=b*b-4*a*c;if(f=1)n*=m;m-;printf(%ldn,n);第24页/共104页4/13/2023Chapter 6 Decision Makin

22、g and Looping 6.36.3 Write a program to compute the sum of the digits of a given integer number.第25页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.3main()long n;int sum=0;printf(Input a positive integer:);scanf(%ld,&n);while(n!=0)sum=sum+n%10;n=n/10;/*number is decreased by 10 times*/printf(

23、The sum of the digits is%d.n,sum);第26页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.46.4 The numbers in the sequence 1 1 2 3 5 8 13 21 are called Fibonacci numbers.Write a program using a do.while loop to calculate and print the first m Fibonacci numbers.(Hint:After the first two numbers in

24、 the series,each number is the sum of the two preceding numbers.)第27页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.4main()long f1=1,f2=1,f=1;int i=2,m;printf(How many numbers do you want to output?);scanf(%d,&m);printf(%12ld,f);while(i=m)printf(%12ld,f);f=f1+f2;f1=f2;f2=f;if(i%4=0)printf(n)

25、;i+;第28页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.56.5 Rewrite the program of the Example 6.1 using the for statement.第29页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.5main()int count,n;float x,y;printf(Enter the values of x and n:);scanf(%f%d,&x,&n);for(y=1.0,count=1;count=n;

26、count+)y=y*x;printf(nx=%f;n=%d;x to power n=%fn,x,n,y);第30页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.66.6 Write a program to evaluate the following investment equation V=P(1+r)n and print the tables which would give the value of V for various combination of the following values of P,r a

27、nd n.P:1000,2000,3000,10,000r:0.10,0.11,0.12,0.20n:1,2,3,8 (I change the value of n for format.)(Hint:P is the principal amount and V is the value of money at the end of n years.This equation can be recursively written as V=P(1+r)P=V That is,the value of money at the end of first year becomes the pr

28、incipal amount for the next year and so on.)第31页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.6main()int n;float p,pp,r,v;char ch;for(p=1000;p=10000;p=p+1000)printf(nPrincipal amount:%.0fnrate ,p);for(n=1;n=8;n+)if (n=1)printf(%d year,n);else printf(%d years,n);for(r=0.10;r 0.21;r=r+0.01)pp

29、=p;printf(n%.2f:,r);for(n=1;n=8;n+)v=pp*(1+r);pp=v;printf(%9.2f,v);printf(nPress enter key to continue.);scanf(%c,&ch);第32页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.76.7 Write programs to print the following outputs using for loops.(a)1(b)*22 *333 *4444 *55555 *第33页/共104页4/13/2023Chapte

30、r 6 Decision Making and Looping 6.7main()/*(a)*/int i,j;for(i=1;i=5;i+)for(j=1;j=1;i-)for(j=1;j=5-i;j+)printf();for(j=1;j=i;j+)printf(*);printf(n);第35页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.86.8 Write a program to read the age of 100 persons and count the number of persons in the age

31、 group 50 to 60.Use for and continue statements.第36页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.8main()int age,count=0,i;for(i=1;i=100;i+)printf(Please input the age of person%d:,i);scanf(%d,&age);if(age 60)continue;count+;printf(The number of persons in the age group 50 to 60 is%d.n,coun

32、t);第37页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.106.9 Write a program to print a table of values of the function y=exp(-x)for x varying from 0.0 to 10.0 in steps of 0.10.The table should appear as follows:Table for Y=EXP(-X)_ x0.10.20.30.9 0.0 1.0 2.0 3.0 9.0_第38页/共104页4/13/2023Chapter

33、 6 Decision Making and Looping 6.10#include main()double x=0;int i,j;printf(nY=EXP(-X)n);for(i=1;i=80;i+)printf(_);printf(n xt);for(i=1;i=9;i+)printf(t0.%d,i);for(i=0;i=9;i+)printf(nn%d.0,i);for(j=0;j=9;j+)x=i+0.1*j;printf(t%.2le,exp(-x);for(i=1;i 0)n=n/2;m+;printf(The equivalent binary of%ld is:,nu

34、mber);for(i=m;i=1;i-)n=number;for(j=1;j i;j+)n=n/2;printf(%ld,n%2);第41页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.126.12 Write a program using for and if statement to display the capital letter S in a grid of 15 rows and 18 columns shown below.*第42页/共104页4/13/2023Chapter 6 Decision Makin

35、g and Looping 6.12main()int i,j;for(i=1;i=4&i=10&i=12)printf(%18s,*);elsefor(j=1;j=18;j+)printf(*);第43页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.136.13 Write a program to compute the value of Eulers number e,that is used as the base of natural logarithms.Use the following formula.e=1+1/

36、1!+1/2!+1/3!+1/n!Use a suitable loop construct.The loop must terminate when the difference between two successive values of e is less than 0.00001.第44页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.13#define ERROR 0.00001main()float e=1,item;int i,m=1;long n;printf(e=1);dofor(n=1,i=1;i=ERROR

37、);printf(=%fn,e);e=1+1/1!+1/2!+1/3!+1/4!+1/5!+1/6!+1/7!+1/8!+1/9!=2.718282第45页/共104页4/13/2023Chapter 6 Decision Making and Looping 6.156.15 The present value(popularly known as book value)of an item is given by the relationship.P=c(1-d)nwhere c=original costd=rate of depreciation(per year)n=number o

38、f years P=present value after n yearsIf P is considered the scrap value at the end of useful life of the item,write a program to compute the useful life in years given the original cost,depreciation rate,and the scrap value.The program should request the user to input the data interactively.第46页/共10

39、4页4/13/2023Chapter 6 Decision Making and Looping 6.15main()float c,d,p;int n,i;printf(The original cost of the item is:);scanf(%f,&c);printf(The rate of depreciation(per year)is:);scanf(%f,&d);printf(The number of years is:);scanf(%d,&n);for(i=1,p=c;i=1&count=5)candidatecount+;elsespoilt+;scanf(%d,&

40、count);printf(The result of voting:n);for(count=1;count=5;count+)printf(Candidate%3d-ballots:%d n,count,candidatecount);printf(Spoilt ballots:%dn,spoilt);第49页/共104页4/13/2023Chapter 7 Arrays 7.47.4 The following set of numbers is popularly known as Pascals triangle.If we denote rows by i and columns

41、by j,then any element(except the boundary elements)in the triangle is given bypi,j=pi-1,j-1+pi-1,jWrite a program to calculate the elements of the Pascal triangle for 10 rows and print the results.第50页/共104页4/13/2023Chapter 7 Arrays 7.4#define N 10main()int pNN=0,i,j;for(i=0;i N;i+)for(j=0;j=i;j+)if

42、(j=0|i=j)pij=1;elsepij=pi-1j-1+pi-1j;for(i=0;i N;i+)for(j=0;j=i;j+)printf(%5d,pij);printf(n);第51页/共104页4/13/2023Chapter 7 Arrays 7.57.5 The annual examination results of 100 students are tabulated as follows:Write a program to read the data and determine the following:(a)Total marks obtained by each

43、 student.(b)The highest marks in each subject and the Roll No.of the student who secured it.(c)The student who obtained the highest total marks.Roll No.Subject 1Subject 2Subject 3第52页/共104页4/13/2023Chapter 7 Arrays 7.5#define N 100main()float sN4=0,mN=0,max;int i,j,k;printf(Please input the roll num

44、ber and marks of%d students.n,N);for(i=0;i N;i+)printf(Roll No.:);scanf(%f,&si0);printf(The mark of Subjcet 1:);scanf(%f,&si1);mi=si1;printf(The mark of Subjcet 2:);scanf(%f,&si2);mi+=si2;printf(The mark of Subjcet 3:);scanf(%f,&si3);mi+=si3;for(i=0;i N;i+)printf(Total marks of student%04.0f is:%.2f

45、n,si0,mi);for(j=1;j=3;j+)for(i=0,max=s0j,k=0;i max)max=sij;k=i;printf(The highest mark of Subject%d is:%.2f.The Roll No.is:%04.0fn,j,max,sk0);for(i=0,max=m0,k=0;i max)max=mi;k=i;printf(The student%04.0f obtained the highest total marks.n,sk0);第53页/共104页4/13/2023Chapter 7 Arrays 7.67.6 Given are two

46、one-dimensional arrays A and B which are sorted in ascending order.Write a program to merge them into a single sorted array C that contains every item from arrays A and B,in ascending order.第54页/共104页4/13/2023Chapter 7 Arrays 7.6#define M 5#define N 6main()int aM,bN,cM+N,i,j,k;printf(Input%d numbers

47、 of array a in ascending order:n,M);for(i=0;i M;i+)scanf(%d,&ai);printf(Input%d numbers of array b in ascending order:n,N);for(j=0;j N;j+)scanf(%d,&bj);for(k=i=j=0;k M+N;k+)if(bj ai&j=M)ck=bj;j+;continue;elseck=ai;i+;continue;printf(The merged numbers are:);for(k=0;k M+N;k+)printf(%6d,ck);第55页/共104页

48、4/13/2023Chapter 7 Arrays 7.77.7 Two matrices that have the same number of rows and columns can be multiplied to produce a third matrix.Consider the following two matrices.The product of A and B is a third matrix C of size nxn where each element of C is given by the following equation.Write a progra

49、m that will read the values of elements of A and B and produce the product matrix C.第56页/共104页4/13/2023Chapter 7 Arrays 7.7#define N 10main()int aNN,bNN,cNN,i,j,k;printf(Please input matrix A of%d by%d:n,N,N);for(i=0;i N;i+)for(j=0;j N;j+)scanf(%d,&aij);printf(Please input matrix B of%d by%d:n,N,N);

50、for(i=0;i N;i+)for(j=0;j N;j+)scanf(%d,&bij);printf(The product of matrix A and matrix B is:n);for(i=0;i N;i+)for(j=0;j N;j+)for(k=0,cij=0;k N;k+)cij+=aik*bkj;printf(%6d,cij);printf(n);第57页/共104页4/13/2023Chapter 7 Arrays 7.87.8 Write a program that fills a five-by-five matrix as follows.Upper left t

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > PPT文档

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com