0% found this document useful (0 votes)
35 views11 pages

C - Lab4 - Eng - Ruqaih Salman Final 2022

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
35 views11 pages

C - Lab4 - Eng - Ruqaih Salman Final 2022

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
false. J true Wel ee ANI I Lal le Ade ae 1. Mark the following statements as true or false. lint sete!) gilatall poet Sas hal OS The reul ofs logicl expresioncommotbc algae to anit varie, eh owe OS «if (Score>=60); Chef he gh pul ay A fe Lots any 431i) « one-way selection ‘Mila Gace if b. Ina one-way selection, if a semicolon is placed after the expression Se) statement as if (score>=60); , the expression in the if statement is always true.. else ste if ke UD a ons ©. Every if statement must have a corresponding else. nee tl gle Ws da oy If Sue a ll el : always evaluates to true. . The expression in the ‘(core = 30) grade ‘ch> ='Z j\'ch && ch <='Z') evaluates to false if either ch < "A &8 ch <='Z) ss eu is oreh e. . The expression: (eh Suppose the input is 5. The output of the code is: Num is zero Num is zero 2.8m 3,8!) cla) 5 9 QS! ol ve A Ly x CAS 1 ad sae 6 [EDO] ae |. The expression {(x > 0) is true only if x is a negative number. Aah Jalye fl] 5 [1] ase ++ Ct © h. InC++, both ! and != are logical operators. 2. Evaluate the following expressions: cfalse el true Waly ya Ut | all cats 13) Le a 6.28/3<3-1.2 abd By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com 3. Suppose that x, y, and are int variables, and x = 10, y = 15, and z = 20. Determine whether the following expressions evaluate to true or false. SE AVA Si ual CaS 1) Lose 2 = 20 sy = 15 9x = 10 Nx > 10) (I= 5) && ( peaoge x=2||e+y>=2) (x <=y- 2) && (y >=7) || @-2!= 20) clint SL kte oe Bhe 2 sy 5 x ol asl false eltrue 4, Suppose that x, y, z, and w are int variables, and x = 3, y = 4, z= 7, and w= 1. What is the output of the following statements? » cout <<" cout << "x != peaos IGN cl lal gl ga Lew cout << "I> w): 15257 sy a4 5x = 356 int ohne a wz sy 9x ol ati y) << endl; ) << endl; +3) << endl; << (z> w) << endl; cout << "x+y = 68) cout << "You pass. else; «<< endl; cout << "You fail." << endl; abd By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com 8. Write C++ statements that output Male if the gender is 'M', Female if the gender is gender otherwise and invalid invalid.» «gender = "F" GS 8) Female 85 s gender = "M" JIS 15 Male cA! gill ++ C Hah gai cS! ally gender 4a oé 9. What is the output of the following program? SAD ali yl gs ga Le winclude using namespace std; int main() { int myNum = 10; int yourNum = 30; if (yourNum % myNum { 3) yourNum = myNum = 15 } else if (yourNum % myNum { yourNum myNum = 2; yourNum myNum = 3; + cout << myNum << return @; << yourNum << endl; SyourNum = 12 5 myNum =5 JS 15) « (9 cpt!) Sal cal gh alt ll ga Le 10. What is the output of the program in previous exercise (Exercise 9) , if myNum =5 and yourNum = 12? SyourNum = 33.5 myNum = 30 JS 15) « (9 ca nl) ell Ca pall G8 a pl Sw 111 What is the output of the program in previous exercise (Exercise 17), if myNum = 30 and yourNum = 33? Laba By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com 12, Suppose that score is an int variable. Consider the following if statements: : : AIF SLLe el fee int ye score 45 Gl Ua il a. 10) couts<"GradeisC."<=y) Z=K-Y; else ZEY-X b. if (hours >= 40.0) wages = 40 * 7.50 + 1.5 * 7.5 * (hours - 40); else wages = hours * 7.50; ©. if (score >= 60) str = "Pass"; else str = "Fail"; 14, Rewrite the following expressions using an if..else statement. (Assume that all variables are declared properly.) (-intganno JS Ol yt! gua Ce ge eaill Sl ye fl) Lf. else She ail Ul Gil all 46 sel a. (<5)? y=10:y=20; b. (fuel >= 10) ? drive = 150 : drive = 30; €. (booksBought >= 3) ? discount = 0.15 : discount = 0.0; 15, Suppose that you have the following conditional expression, (Assume that all the variables are properly declared.) (gone JS cil gg Ce cu nal pl al) gh) yal al) i Laba By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com (0 < backyard && backyard <= 5000) ? fertilizingCharges = 40.00: fertilizingCharges = 40.00 + (backyard - 5000) * 0.01; © backyard=3000 448 cals |} fertilizingCharges + a le a, What is the value of fertilizingCharges if the value of backyard is 3000? © backyard=5000 448 <5 | fertilizingCharges 448 A Leb c. What is the value of fertilizingCharges if the value of backyard is 5000? £ backyard=6500 48 Suis \3) fertilizingCharges +8 2 Le dd. What is the value of fertilizingCharges if the value of backyard is 6500? 16, State whether the following are valid switch statements. If not, explain why. Assume that n and digit are int variables, Cl gia digit 91n OI Ga sll call Cpt « AIS GS pL) allen Jia SI he Ge She A) SI jhall CAS 1] L se, -int a. switch (n <= 2){ case @: cout << "Draw." << endl; break; case 1: cout << "Win." << endl; break; case 2: cout << “Lose.” << endl; break; iE b. switch (digit / 4) { case 8, case 1: cout << “low.” << endl; break; case 1, case 2: cout << "middle." << endl; break; case 3: cout << “high.” << endl; ¢. switch (n % 6) case 1: case 2: case 3: case 4: case 5: cout << n3 break; case @: cout << endl; break; Laba By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com + d.switch (n % 10) { case 2: case 4: case 6: case 8: cout << "Even"; break; case 1: case 3: case 5: case 7: cout << "Odd"; break; + 17.8 that alph: it variable. Cons nppoes thot apne isan it variable, Gon alt eal er the following C++ code: : $4 Casale! i ae int yi alpha oi vail cin >> alpha; switch (alpha % 6) { case @: alpha--5 break; case 1: case 2: alpha = alpha * 25 break; case 3: break; case 4: alpha = case 5: alphat+; break; default: alpha = } alpha - 55 alpha / 3; a. What is the output if the input is 8? b. What is the output if the input is 3? $8 JAY! JS gill a £3 DA2Y! GS i] gill ya Le aba By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com ¢. What is the output if the input is 17? 117 Das! JS 13) gil a Le d. What is the output if the input is 24? $24 Js! GIS 13) Gill a 18. n the following code, correct any errors that would prevent the program from compiling or running: Sail lanl Cg geal pall gia ob Ua! gl quate lll SI) include main() { int num1, num2; bool found; cout << "Enter two integers: ; cin >> num1 >> num2; cout << endl; if (num1 >= num2) && num2 > 2 switch (num % num2) { case 1 found = (num / num2) >= 63 break; case 2: case 3 num = num2 / 2; brake; default: num2 = numd * num2; + else { found = (2 * num2 < num1); if found cin >> num2 num 1 = num2 ~ num1; temp = (num1 + num2) / 16; if num2 { num = num2; num2 = temp; H cout << numi <<" " << num << endl; 3} Laba By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com After correcting the code, answer the following questions. (If needed, insert prompt lines to inform the user for the input.) pokhud sey lines prompt 4flaa) slau! Jest «513 1) APM LAY Ge Gag Glad) a6] gy ony (dary a. What is the output if the input is 10 8 6?» S271 JS 13) gall we b. What is the output if the input is 4.9.11)? Jss¥! dS 14 ull wl 19. Suppose the input is 3. What is the value of beta after the following C++ code executes? Sold) C +48 MS beta Led a 3 8 Jay ol Le cin >> beta; switch (beta) { case 3: beta = beta + 3; case 1: betat+5 break; case 5: beta = beta + 5; case 4: beta = beta + 4; + 20. Suppose the input is 6. What is the value of a after the following C++ code executes? iz fall CHAS Mi aw a ga 6s DRY GI a cin >> a if (a > @) switch (a) { case 1: a=a+ 3; case 3: att; break; case 6: a=a+6; aba By Eng: Ruqai Salman rugaihw3 salmanw3@ gmail.com case 8: aza*s; break; default t else aza+2 21. The following program contains errors. Correct them so that the program will run and output w =21. W = 21 class qa nll GES py Cans Lgmsmety Bolas! le Il ali ll og ng #include using namespace std; const int SECRET = 5 main() { int x, y, w, 25 z=95 if z2> 10 xX = 12; y = 5, w= x + y + SECRET; else x y = 4, w= x + y + SECRET; cout << << w << endl; } 22. write a C++ program to ask student to enter his grade then print the result “pass” or “failed” Bays DAN say Gaal ol ald ALN GIS 1) Gaon aay CaS 23. write a C++ program to print the number if the number is divided by 5 § Bacall le Raul) Jud saat GAS 18) Gaal gal» cS! 24. (Guess game) write a C++ program to initial value in variable then ask the user to guess number if the number which the user guess equal your number print * you win’ else print * you lost” (Gus85 Sp )2g60) Sts pall Riga gl sal) id poll J ge Sell sal GIS 1 Gye aly 8) abd By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com 25.Write a C++ program That calculates the rate of student If the grade >=90 print “Excellent” €te, $ switch plssiuh s joy if SS 8 yo psill dolly Ub dy ys JAY gai GS! 26. Write a C++ program doing a traffic signal if the red prints stop whether yellow prints prepared if the Green prints move Vi gh Lhasa dy aM la AOU eal yl SI Real elang yy ell I pl aly SI Seal 27.Write a C++ program to solve the second degree equation ( Note :use #include with sqrt() function). 2 #includecmath.h> eSi3 yas pial) gle Jpaall bade PEt ead Jal eal iS! Xt=-besqrt(delta) /2* ks Gay.) SSRN sqrt () Ul sus aX? +bX + c= Ofor any real a, b and ¢ Ifyou know x=-b+ yi /2%a A=b*b - 4tatc 1f.>0 you find two solution Xteb+ VE 12a X2eb- VE 12a If.1=0 you find one solution Xebitta If. <0 you cannot find any solution 28.Write a C++ program that scans the state of the atmosphere if it hot or cold or cool G4 BLESS Gly Cold yal 73.5 12 cu OS 11 HOt ed 73 G+ p51 CAS 1 AdaaN 5 J yall a 2 Gea way OSS! © cool ui 12 29.write a C++ program that checks if the user enters the dimensions of square or rectangle aba By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com SAM pl ga le Lay Juin gl gee GIS 1 IGE aa aa aly Ca 30. Write a C++ program which calculates the age group(child , young ,old) © Syne hs gl ile GIS 13) Saal joall anil gall y ci! 31.Write a C++ Program like a calculator for the basic operations, the user enter two numbers and operation the program calculate the result. Rewrite the same program with Switch switchpsicb Shall ea pl) AMS 20 8 Helse ifplSiub Uae Sule al peel ely CSS) 32.Write a C++ Program that calculates the volume of blood if it is a natural between 4-6 AB oe SS Hel lS GI Cam 8S ell fe gag Gall US I Gena gal a. If less is suffering from a decrease in blood 2 iB coe lly 4b I, aS ola a GB cas 13) b. Ifit has more than an increase in blood volume blood BS co lly A lly yas oa ya 51 cs 1 33.Write a C++ program to print * Saturday’ if the user enter 1 “Sunday” if the user enter 2 ... etc. Write above program twice by using a. (if ...else if)’ b. (switch) switchalssiah cal AUS se) iF Sat Hag pak ay pals Schade Dy g pl egy LLY eal cS! aba By Eng: Ruqaih Salman rugaihw3 salmanw3@ gmail.com

You might also like