more while loop examples cs303e: elements of computers and programming

8
More While Loop Examples CS303E: Elements of Computers and Programming

Upload: nigel-mcdonald

Post on 03-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: More While Loop Examples CS303E: Elements of Computers and Programming

More While Loop Examples

CS303E: Elements of Computers and Programming

Page 2: More While Loop Examples CS303E: Elements of Computers and Programming

Quote of the Day

Tell me and I forget. Show me and I remember. Involve me and I understand.

- Anonymous Chinese Proverb

Page 3: More While Loop Examples CS303E: Elements of Computers and Programming

Question:Nested If StatementsWhat is the expected

output?if(1<10 and 6<7): if(8>=10 or -1>0): print “if if” else print “if else”elif(4==4 and 9>=6): print “elif”

A. if ifB. if elseC. elifD. None of

the above

Page 4: More While Loop Examples CS303E: Elements of Computers and Programming

Exercises

Write a program that calculates the product of the first n odd numbers (beginning at 0).

Write a program that reads n positive integers from the keyboard and then prints the maximum and minimum to the screen.

Page 5: More While Loop Examples CS303E: Elements of Computers and Programming

Group Exercise

A perfect number is a number that is the sum of its divisors. The divisors of 6 are 1, 2, and 3 and they add up to 6 (1+2+3=6). Another perfect number is 28. The divisors of 28 are 1, 2, 4, 7, 14 and they add up to 28. There are not many perfect numbers. Write a program that takes as input an integer number and determines if it is perfect or not.

Page 6: More While Loop Examples CS303E: Elements of Computers and Programming

Exercises

Prompt the user to enter exam scores, entering a negative number to quit. Find the average and print it.

Page 7: More While Loop Examples CS303E: Elements of Computers and Programming

Exercises

Write a program that determines how long it takes an investment to double at a given interest rate. The input is the amount of the investment and an annualized interest rate. The output is the number of years it takes an investment to double.

Page 8: More While Loop Examples CS303E: Elements of Computers and Programming

Question:While Loops

When would you use a while loop?

A. When you know exactly how many times the loop should execute

B. When the loop will execute an indefinite number of times