Sei sulla pagina 1di 5

Assignment #2

Create a Windows Forms Application that performs some while loops, if-else statements and double
type calculations.

Loop 1

Inside a panel, use labels and textboxes to get a starting number and an ending number from
the user to be used for the loop.
Insert a button called Go that will run the loop, and an output label that will display
information once the loop is done.
Insert a button beside the start number textbox called Is Prime?

Part 1
Use the double type for all mathematical variables.
Create a while loop using the first textbox as the starting number. Incrementing by 1, run the loop until
you reach the value of the end number (from the second textbox).

Part 2 Inside the Loop


Create a double type variable that tallies the total number of odd numbers.
Create a double type variable that tallies the total number of even numbers.
Create a double type variable that keeps a running total of the sum of all odd numbers.
Create a double type variable that keeps a running total of the sum of all even numbers.

Use an if-else statement to determine if a number is odd or even. Use this method below which returns
a Boolean (true or false).

public bool IsOdd(double value)


{
return value % 2 != 0;
}
Create a double type variable that gets the sum of all the numbers in the loop. Ex) 1 -> 5 would be
1+2+3+4+5 = 15.
Create a double type variable that gets the product of all the numbers. Ex) Loop 1 -> 5 would be
1*2*3*4*5 = 120

Part 3 Calculations
Create a double type variable that calculates the average of all the numbers in the loop. Ex) sum of all
the numbers / number of times through loop.
Create a double type variable that takes the average and divides it by the sum of all the numbers.
Format both doubles to have 2 decimal places. Ex) average.ToString(F2)

Part 4
Display all the results onto the output label like so:

Part 5
When the Is Prime? button is clicked, send a message to the user via the MessageBox.Show command
that tells them if the start number is a prime number or not. Use an if-else statement to determine this.
Use the method below to determine if the number is a prime or not. It returns a Boolean (true or false).

public bool IsPrime(int n)


{
if (n > 1)
{
return Enumerable.Range(1, n).Where(x => n % x == 0).SequenceEqual(new[] { 1, n });
}
return false;
}

Loop 2 Progress Bar


Part1

Inside a panel, use labels and textboxes to get an ending number from the user to be used for
the loop.
Insert a button called Go that will start the loop.
Insert a Progress Bar
Insert a BackgroundWorker
Insert an output label that will display information once the loop is done.

Part 2
Create a while loop using the textbox as the ending number. Starting at 1, increment by 1 until you
reach the value of the end number.
Set the progress bar maximum to the value of textbox (end number).
Put a System.Threading.Thread.Sleep(500); inside your loop to create a small pause each time through.

Part 3
In the backgroundworker processchanged method, output the current Date and Time to the output
label. To get the current Date and Time use DateTime.Now.ToString().

***The Progress Bar example is on D2L in Chpater3->Code->chap3_2_Examples***

Potrebbero piacerti anche