Unformatted text preview: 2. for 1 ← i to m 3. if n is even 4. 3← n n 5. else 6. n n 2 ← 7. print n 8. print i 9. stop 2. (8 Points) Write an algorithm in pseudocode which takes a positive integer n from the user as input, then prints out the sum of all the odd integers in the range 1 to n . If n itself is odd, then n is included in the sum, while if n is even, it is not included in the sum. Thus if the user enters either 5 or 6, the sum 9 ( 5 3 1 + + = ), is printed. Solution: There are a number of valid solutions, one of which is: 1. get a value for n from the user 2. sum ← 3. 1 ← i 4. while n i ≤ 5. sum ← sum + i 6. 2 + ← i i 7. print sum 8. stop...
View
Full
Document
This note was uploaded on 09/15/2011 for the course CMPS 10 taught by Professor Tantalo during the Fall '08 term at UCSC.
 Fall '08
 TANTALO

Click to edit the document details