80 likes | 202 Vues
This content explores key programming concepts including while loops and nested if statements, essential for CS303E: Elements of Computers and Programming. It provides a thought-provoking quote on the importance of involvement in learning, discusses nested conditionals with an example, and presents exercises such as calculating the product of odd numbers, finding the maximum and minimum of user inputs, and determining perfect numbers. This resource is ideal for developing problem-solving skills in programming.
E N D
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
Question:Nested If Statements What 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 if B. if else C. elif D. None of the above
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.
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.
Exercises • Prompt the user to enter exam scores, entering a negative number to quit. Find the average and print it.
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.
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