Lab 6. Loops
Write an application that calculates a factorial.
The factorial of a number is the product of all integers from 1 to the
number. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 which equals 120.
You will need the following:
- A textbox to input an integer from the user.
- A label to output the factorial of the integer entered by the user.
- A button to begin the calculation.
You should enter the code to do the calculation and produce the output
within the button's click event function.
Writing the code:
- Use a For loop that counts from 1 to the number input by the user in
steps or increments of 1.
- At minimum, you will need to declare variables that keep track of the
loop count and the factorial product.
- The statement within the For loop should multiply the current loop count
value to the current factorial product and make that the new factorial
product.
- When the loop finishes, output the value of the factorial product to the
label as shown in the picture above.
Program 2: Power To
Write an application that calculates x^y or x to the power of y.
x to the power of y is x multiplied by itself y times. For example, 3 to the
power of 4 is 3 * 3 * 3 * 3 which is 81.
You will need the following:
- A textbox to input an decimal for the base (x).
- A textbox to input a positive integer for the power (y).
- A label to output the value of x^y.
- A button to begin the calculation.
You should enter the code to do the calculation and produce the output
within the button's click event function.
Writing the code:
- For this application we will do some minor input validation.
- We will only accept non-negative integers for the power input.
- If the power input is negative, output a message box telling the
user they must input a non-negative integer for the power.
- Recall that you can construct a simple message box with
the following line:
MsgBox("Power input must be a non-negative integer")
- You should not do the calculation if the power is negative.
- If the power is non-negative (in other words, the Else part of the If
mentioned above), then use a For loop to calculate the
result.
- You will again need at minimum a loop count variable and a variable to
keep track of the product of the base multiplied by itself.
- When the loop finishes, output the value of the product to the
label as shown in the picture above.
Suggested algorithm
Declare and initialize variables to appropriate values
If power is negative
Construct msgbox with error message
Otherwise
Loop power(y) times
product = product * base(x)
Next
Output final product to label
End if
Program 3: Night Sky
Write an application that draws the stars and a crescent moon in a picture
box.
Draw this to a picture box the same way you did the smiley in the last lab.
This time, however, you will use a for loop to randomly draw 200 stars.
You will need the following:
- A picture box with the dimensions 600 by 300.
- Change the background color of the picture box to black (web colors)
using the properties window.
You should enter the code to draw the stars and moon within the picture
box's click event function.
Writing the code:
- You will need at minimum a loop count variable and variables to store
the x and y coordinates for each star.
- You will also need a SolidBrush object (refer to
lab 6
if you forgot how to construct this).
- The order for drawing these next shapes matters:
- Draw a full moon (fillEllipse). I used the color Wheat for my
SolidBrush here.
- Change the SolidBrush's color to Black for the next output to
match the pic box's background color.
- Draw a full moon again (fillEllipse again), but this time draw it
with an x coordinate value moved to the left by 10 units.
- Try this out now to see your crescent moon before moving on to draw the
stars.
- Now to draw the stars:
- We will need to use the Rnd() function as we did in class.
- Recall that the expression:
Int((upperbound - lowerbound + 1) * Rnd()) + lowerbound
produces random integers in the range from lowerbound to upperbound.
- To output 200 stars we need a For loop that loops 200 times, each
time getting new random values for the x and y coordinate for one
star.
- Since our x range of the picture box is 0 to 600, that is the
lower and upper bounds when getting the x coordinate.
x = Int((600 - 0 + 1) * Rnd()) + 0
- Do the same for the y coordinate but with the range 0 to 300.
- To actually draw the star, you could use a fillEllipse function
that uses these random x and y coordinates and has a width of 1 and
a height of 2.
- You do not need to draw the lightning bolt.
Bonus: Up to 20% possible
Draw a random lightening bolt to your night sky using a For loop.
- Draw a small 1 unit vertical line within the body of the For loop. In
other words, the y coordinate of the beginning of each line will be the loop
count value and the y coordinate of the end of each line will be 1 unit
larger than the beginning.
- Each time through the loop, you should randomly move the line in the x
direction with a range of -1 to 2 (that is why the line slowly moves to the
right).
- Don't forget to keep the old value of x and just randomly add -1 to 2
units to that value: x = x + ...