CS2263-Ex2-IfThenElseLoopsStr-S23-v1

docx

School

Idaho State University *

*We aren’t endorsed by this school

Course

MISC

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

2

Uploaded by DukeDolphin3085

Report
CS2263 S23 Exercise 2- IfThenElse, Loops and Strings Create a visual studio 2022 windows form project (not another type of project) named <yourfullname>2263Ex2, and a doc file named <yourLastName>2263Ex2.doc for your written answers to some of the problems. Zip and submit on the course moodle page when working correctly. Use appropriate textbox, button, etc prefixes and names, with great descriptions in the buttons. 1. (15 points) Add a button, labels, a textbox (named txtIn) and a label (lblOut), that when given a year in the txtIn box, indicates whether it is a leap year in lblOut. Use IfthenElses and Boolean logic to determine whether the year is a leap year, not some built in gadget (it actually can be done as a single Boolean expression). A year is a leap year if it is evenly divisible by 4, except it is not a leap year if it is evenly divisible by 100, except it is a leap year if it is evenly divisible by 400. Thus 1996 and 2000 were leap years, but 1900 was not. 2. (5 points) What input must be place into txtIn to the following procedure for it to display 22 in txtOut with the button is depressed? Place your answer in the word file. private void btnTask2_Click( object sender, EventArgs e) { int n, sum = 0; n = int .Parse(txtIn.Text); for ( int i = 1; i <= n; i = i + 3) { sum += i; } txtOut.Text = sum.ToString(); } 3. (5 points) What input must be place into txtIn to the following procedure for it to display 120 in txtOut with the button is depressed? Place your answer in the word file. private void btnTask3_Click( object sender, EventArgs e) { int n, i = 1, product = 1; n = int .Parse(txtIn.Text); while (i < n) { i++; product = product * i; } txtOut.Text = product.ToString(); } 4. (15 points) Fibonacci’s sequence starts with values 1 and 1. The next value in the sequence is formed by summing the previous two values. Thus the third value is 1+1=2, the fourth is 1+2=3, the fifth is 2+3=5, etc. Thus, the sequence is 1, 1, 2, 3, 5, 8, 13, 21, …. Create a button with code that will obtain a positive integer from txtIn (test to make sure positive) and will display the sum
of the first n numbers in the Fibonacci sequence. If n is not positive, place an error message in txtOut. 5. (5 points) SquareCode. Implement the following code in a button’s click event method using a textbox with the multiple line property set to true. private void btnTask4_Click( object sender, EventArgs e) { string s = "" ; for ( int r = 0; r < 10; r++) { for ( int j = 0; j < 10; j++) s += "*" ; s += "\r\n" ; } txtMultiLineOut.Text = s; } 6. (15 points) Based on the code and techniques in #3, generate a “firstTriangle” button that obtains the number of lines from txtIn and displays the following triangle in the txtMultipleLineOut assuming n=5. * ** *** **** ***** 7. (20 points) Based on the code and techniques in #3, generate a “SecondTriangle” button that obtains the number of lines from txtIn and displays the following triangle in the txtMultipleLineOut assuming n=5. ----* ---** --*** -**** ***** 8. (20 points) Write a code fragment under a button (with an appropriate name) that inputs a positive integer n (n<=26) from the user and creates a string containing the first n lower case letters of the alphabet. Use a loop rather than a complex “if” statement. Note that ‘b’ == (char) ((int)‘a’ + 1) in C++.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help