Main.Lab3 History
Hide minor edits - Show changes to markup
- Practice iteration structures: while and for
- Practice while loops
Write a function called factorial(n) that computes n!.
Write a function called factorial(n) that returns n!.
Write a version that uses a while structure, and a version that uses a for loop.
that sums the even numbers that are less or equal to n.
that returns the sum of the even numbers that are less or equal to n.
Print numbers
Write a function called print_numbers(n) that prints a comma-separated list of the numbers 1 through n on a single line.
So for example, print_numbers(5) should produce the output
1, 2, 3, 4, 5
Note that the print command starts a new line every time it is issued. To prevent it starting a new line do
print something,
The comma at the end tells it not to start a new line. To print a number followed by a string you can do the following:
print str(2) + "string"
Factorial
Factorial
Summing even numbers
Summing even numbers
Lab 3
Objectives:
- Practice iteration structures: while and for
Factorial
Write a function called factorial(n) that computes n!.
Recall that n! = n (n-1) ... 1, and that 0! = 1.
Write a version that uses a while structure, and a version that uses a for loop.
Summing even numbers
Write a function called sum_even(n)
that sums the even numbers that are less or equal to n.
