CS 1500-1, Spring 2010
Lab 3
Fun with Loop in Two Parts!

### Part 1

Write a program that sums the numbers from 1 to n, inclusive, where n is input by the user. For example, if I give you three, your program will compute 1+2+3 and output 6.

A good way to do this is to write a loop. The question is which loop to choose? Let's experiment with some of the options. Try it with 4 different loops:
• for loop with increment (1+2+3)
• for loop with decrement (3+2+1)
• while loop with increment (1+2+3)
• while loop with decrement (3+2+1)
Design your output to show the results of each loop separately.

Individual steps:
1. Think through how you might sum n numbers. One way to solve this is with a loop. Write a program that takes a value n from the user and adds up the numbers from 1 to n inclusive. You program should use four different loops: for loop with increment, for loop with decrement, while loop with increment, while loop with decrement. Then print the output value of each loop to the screen.

2. Make a script (call it lab03_loop.script) testing your program for at least n = 100 (more testing would be better, see section 3.4 of out book). Be sure to filter the script and directed at the bottom of the script page.

3. Upload your source code (lab03_loop.cpp) and your script (lab03_loop.script) to the CSHomework system. Turn in a hard copy of your source code.

### Part 2

The goal of this exercise is to develop an interactive program that uses loops.

A prime number is a number greater than 1, whose only divisors are 1 and itself.

Write a program that finds and prints all prime numbers less than or equal to n, where n is a number input by the user. A template for starting the program is here. A script that shows the required format of the output is available here.

Wikipedia
MathWorld

Individual steps:
1. Think through how you might check for primes. One way to solve this is with a doubly-nested loop. The outer look will iterate from 2 to n, while the inner loop checks to see if the current integer is prime by checking for divisors. You may use the template provided to help you organize your program.

2. Make a script (you could call it lab03.script) testing your program for n = 100. Be sure to filter the script and directed at the bottom of the script page.