Difference between revisions of "Looping Exercises"

From WLCS
Line 6: Line 6:
  
 
'''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
 
* Use while loops to generate the following sequences
  
  
 
* Powers of 2
 
* Powers of 2
 +
** Print out the first N terms of the sequence
 
** 1, 2, 4, 8, 16, 32, 64, 128, ...
 
** 1, 2, 4, 8, 16, 32, 64, 128, ...
  
 +
* 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
 +
 +
* Fractions Sequence
 +
** Print out the first N terms of the sequence
 +
** 1/1, 1/2, 1/3, 1/4, 1/5, ...
  
 
* Fibonacci Sequence (HINT: use multiple variables)
 
* Fibonacci Sequence (HINT: use multiple variables)
 +
** Print out the first N terms of the sequence
 
** 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
 
** 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
 
  
 
* Factorial Sequence (HINT: use multiple variables)
 
* Factorial Sequence (HINT: use multiple variables)
 +
** Print out the first N terms of the sequence
 
** 1, 1, 2, 6, 24, 120, ...
 
** 1, 1, 2, 6, 24, 120, ...

Revision as of 13:41, 16 October 2012

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, ...
  • 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
  • Fractions Sequence
    • Print out the first N terms of the sequence
    • 1/1, 1/2, 1/3, 1/4, 1/5, ...
  • 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, ...