C语言编程练习.pptx
《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;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 编程 练习
限制150内