Difference between revisions of "Looping Exercises"

From WLCS
Line 8: Line 8:
 
* For each of the following sequences, '''first prompt the user for a number N'''
 
* For each of the following sequences, '''first prompt the user for a number N'''
 
* 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
 
** 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, ...
 +
 +
* 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
 
* Summation up to N
Line 18: Line 21:
 
** Example: N = 3 would total 6
 
** Example: N = 3 would total 6
 
** Example 2: N = 10 would total 55
 
** 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)

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