Difference between revisions of "Looping Exercises"

From WLCS
(New page: '''Objective:''' * To become well-learned in the way of the while loop '''Directions:''' * For each of the following sequences, first prompt the user for a number N and print out the firs...)
 
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
'''Objective:'''
 
'''Objective:'''
 
* To become well-learned in the way of the while loop
 
* To become well-learned in the way of the while loop
 +
 +
'''Resources:'''
 +
* [[Media:PythonWhileLoops.pptx]]
 +
* [http://openbookproject.net/thinkcs/python/english2e/ch06.html HTTLACS: Ch 6 - Iteration]
  
 
'''Directions:'''
 
'''Directions:'''
* For each of the following sequences, first prompt the user for a number N and print out the first N terms of the sequence
+
* For each of the following sequences, '''first prompt the user for a number N'''
* Creating functions for the following exercises is optional
+
* Use while loops to generate the following sequences
* Powers of 3
+
 
** 1, 3, 9, 27, 81, ...
+
* '''Powers of 2'''
* Fibonacci Sequence
+
** Print out the first N terms of the sequence
** 1, 1, 2, 3, 5, 8, 13, 21, ...
+
** 1, 2, 4, 8, 16, 32, 64, 128, ...
* Factorial Sequence  
+
 
** 1, 1, 2, 6, 24, 120, ... (HINT: using nested loops might help)
+
* '''Fractions Sequence'''
 +
** Print out the first N terms of the sequence
 +
** 1/1, 1/2, 1/3, 1/4, 1/5, ...
 +
 
 +
* '''Summation up to N'''
 +
** Use a loop to add up all the numbers from 1 up through N
 +
** Example: N = 3 would total 6
 +
** Example 2: N = 10 would total 55
 +
 
 +
* '''Fibonacci Sequence (HINT: use multiple variables)'''
 +
** Print out the first N terms of the sequence
 +
** 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
 +
 
 +
* '''Factorial Sequence (HINT: use multiple variables)'''
 +
** Print out the first N terms of the sequence
 +
** 1, 1, 2, 6, 24, 120, ...

Latest revision as of 09:28, 20 December 2016

Objective:

  • To become well-learned in the way of the while loop

Resources:

Directions:

  • For each of the following sequences, first prompt the user for a number N
  • Use while loops to generate the following sequences
  • Powers of 2
    • Print out the first N terms of the sequence
    • 1, 2, 4, 8, 16, 32, 64, 128, ...
  • Fractions Sequence
    • Print out the first N terms of the sequence
    • 1/1, 1/2, 1/3, 1/4, 1/5, ...
  • Summation up to N
    • Use a loop to add up all the numbers from 1 up through N
    • Example: N = 3 would total 6
    • Example 2: N = 10 would total 55
  • Fibonacci Sequence (HINT: use multiple variables)
    • Print out the first N terms of the sequence
    • 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
  • Factorial Sequence (HINT: use multiple variables)
    • Print out the first N terms of the sequence
    • 1, 1, 2, 6, 24, 120, ...