Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "More While Loop Examples CS303E: Elements of Computers and Programming."— Presentation transcript:

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

2 Quote of the Day Tell me and I forget. Show me and I remember. Involve me and I understand. - Anonymous Chinese Proverb

3 Question: Nested If Statements What is the expected output? if(1<10 and 6<7): if(8>=10 or -1>0): if(8>=10 or -1>0): print “if if” print “if if” else else print “if else” print “if else” elif(4==4 and 9>=6): print “elif” print “elif” A. if if B. if else C. elif D. None of the above

4 Exercises Write a program that calculates the product of the first n odd numbers (beginning at 0). 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. Write a program that reads n positive integers from the keyboard and then prints the maximum and minimum to the screen.

5 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. 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.

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

7 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. 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.

8 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


Download ppt "More While Loop Examples CS303E: Elements of Computers and Programming."

Similar presentations


Ads by Google