Homework #5

Topic:Control Flow

Course:CS 102 – Basic Programming

Date Assigned:Sep 30, 2009

Date Due:Oct7, 2009

Reading:None this week

1)This week you only have one programming assignment for homework. You are to write a program that displays prime numbers.

The program should contain a Up/Down control so that you can choose a number. Make sure to set the property so that the minimum value in the up/down control is 2.

You should add a list box control to the form. Make sure to give this control (and all of the controls) good names.

You should also add a button to the form. When the button is pressed (Click event), the program should find all of the prime numbers that are less than or equal to the number in the Up/Down control.

For example, if the user enters “5” in the control and presses the button, the list box should show 2, 3, and 5 (since they are the prime numbers that are less than or equal to 5.

Each time you press the button, remember to clear out the list box from prior executions of the program, or the list box will get pretty full!!!

A few words about finding the prime numbers. Clearly you need a loop (For loop or While loop both work) to test each integer from 2 (1 is not prime!) up to the number in the up/down control.

Then, for each number you’re testing, you need to see if it has any factors other than 1 and the number itself.

For example, 11 is prime because only 1 and 11 divide evenly into 11. But 15 is NOT prime because 1, 3, 5, and 15 all divide evenly into 15.

Last helpful tidbit: How do you know if a number (say variable intLoop divides evenly into another number (say variable intTest)??

If intTest / intLoop = Int(intTest / intLoop), then intLoop is a factor of intTest. For example, 5 is a factor of 15 because 15/5 = Int(15/5) (Int only returns the integer part of the division). Questions? Feel free to ask…

2)Trivia question (Subject: Movies): Can you name the only movie to ever win the “Best Picture” Oscar where the title of the movie contains only two letters? Please don’t google it – that’s just too easy!