Classwork On While Loops

You might also like

Download as pdf
Download as pdf
You are on page 1of 2
Clasevorxt on while loop examples. In the guess-a-nunber problen identity the initialization statements for both inner and outer loops. Also identify the statenents within ‘each oop that update the value of a variable in the while condition. 2. In the cube problen, explain whet will happen if the following changes a. If the rpriner statement ie moved to the Last statement within the oop. b. Tf the n= n+ 2 statement is placed after the cube = n°2 statement 3. type in the cubeRoot program. Place coments at each Line to explain What each statement does. ‘Thon enclose the sestenents in an outer while loop to allow additional 3/26/19 1:50 PM _H:\BNGR297\quess_a_numbe: Loft guese_a_nunbor.m 4A randon integor Detwden 1 and 20 Le generated. ¥ the user {¢ given unlimited chances co guess the nunber. cle play = "y's wolleiplay <= ty") fpunber = vandi (20) Eprintt ("I em thinking of = number between 1 and 20.\n") guess = -1; WeLi6 (gueae-nnumber) guess = input (‘Enter ¢ guess. "17 SE (guesscnimber) Eprintt{'Sorzy. Your guass 12 too 1ov.\n") eteett (guess>munber} Fprinte( Sorry. Your guess ‘x too Mgh.An!): end Eprint£(Congraturaeions! you have gues: Eprint#("The number war d.\n', saber) Epeintt ("D0 you want 0 play again?\n") play = inguc('Enter y for yes or n Zor not ',"s") sd correctly! \a\n") 3/26/19 1:50 PM _H:\ENGR297\cubeTable.m § cubetabie.n § mis program prints a table of integers and their cubes, stopping at the $ tast cube before 700 Ipeinet("Table of Integers and Cubes Less then 700\n") Epeintr(integer\t\t Cabe\n') new ‘cube = 1p waile (cube < 700) forint l{'R4.0£ 810.0E\n", np cubed n= ntl cube = 0°3; Bee are F Serato ae i lta tatters ose baalttach an ae Ane oat ileceaimaa cio Present yee acme suave cninnatcbiroaomer rian Sareea aa ater haces create Spee meses by 6 fet isan tan 0000 Use the program to calculate: @ 306 ©) © v3 3/26/13 1:27 eM #:\ENGR297\cubeRoot .m & euneroot.a 7 calculating successive ostinates. | This ds problen 30, p. 227 in Gilat. = input ("Enter the number for which the cube zoct 42 to be estimated. "11 wien = PF nite (E> 0.00001) rola = sour wiew = (2/(H01d"2) + 24x01@)/3 [Beabs| (x¥au-x014) /x01d) + sprint€ ("the astinate for the cube root iz 8.ae\n', Nev) sprinte ("Using the fonction athrost, the valve 42 €.3f\n', throot (2,3)) Loft Loft

You might also like