New version page

IPFW CS 590A - Loops Worksheet 2

Upgrade to remove ads

This preview shows page 1-2 out of 5 pages.

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
Unformatted text preview:

Unit 3 - Loop Worksheet 2.pdfUnit 3 - Loop Worksheet 2 Answers.pdf9/21/2016 CSC161 Intro to CS in Java  Loop Worksheet 2http://cs.middlesexcc.edu/~schatz/csc161/worksheets/loop.more.worksheet.html 1/2Loops Worksheet 2Show the output produced by the following segments of code. Assume thatall variables are declared as int.1. for (int k = 1; k <= 2; k++) for (int m = 1 ; m <= 3; m++) System.out.println(k + " " + m);2. for (int d = 6; d >= 4; d) for (int e = 2; e <= 4; e++) System.out.println(d + " " + e);3. x = 1; while (x <= 5) { sum = 0; y = 1; while (y <= x) { sum += y; y++; } System.out.print(sum + " "); x++; }4. for (i = 1 ; i <= 5 ; i++) { System.out.println(i); for (j = i ; j >= 1 ; j = 2) System.out.println(j); }5. for (i = 1 ; i <= 3 ; i++) for (j = 1 ; j <= 3 ; j++) { for (k = i ; k <= j ; k++) System.out.println(i + j + k); System.out.println(); }6. for (i = 3 ; i > 0 ; i) for (j = 1 ; j <= i ; j++) for (k = i ; k >= j ; k)9/21/2016 CSC161 Intro to CS in Java  Loop Worksheet 2http://cs.middlesexcc.edu/~schatz/csc161/worksheets/loop.more.worksheet.html 2/2 System.out.println(i + " " + j + " " + k);7. Write statements that will read numbers and print out the sum of thenumbers. The program should stop when the user enters 999, or afterreading 5 numbers, whichever comes first.8. Test your program from problem 7 with the following data: 10 20 30 9999. Test your program from problem 7 with the following data: 10 20 30 40 50 60 7010. Write a program segment using nested loops to create the followingoutput:*********************************************11. Write a program segment using nested loops to create the followingoutput: * *** ***** ****************12. Prompt the user to enter y or n. If the user enters an invalidcharacter, keep reprompting until a value character is entered.Use a while loop.13. Redo problem 12 with a dowhile loop.Email Me | Office Hours | My Home Page | Department Home | MCC Home Page © Copyright Emmi Schatz 20139/21/2016 CSC161 Intro to CS in Java  Loop Worksheet 2 Answershttp://cs.middlesexcc.edu/~schatz/csc161/worksheets/loop.more.worksheet.ans.html 1/3Answers: Loops Worksheet 21. 1 1 2. 6 2 1 2 6 3 1 3 6 4 2 1 5 2 2 2 5 3 2 3 5 4 4 2 4 3 4 43. 1 3 6 10 154. 1 1 2 2 3 3 1 4 4 2 5 5 3 15. 1 1 1 1 2 1 1 2 2 1 3 1 1 3 2 1 3 3 2 2 2 2 3 2 2 3 3 3 3 36. 3 1 3 3 1 29/21/2016 CSC161 Intro to CS in Java  Loop Worksheet 2 Answershttp://cs.middlesexcc.edu/~schatz/csc161/worksheets/loop.more.worksheet.ans.html 2/3 3 1 1 3 2 3 3 2 2 3 3 3 2 1 2 2 1 1 2 2 2 1 1 17. System.out.print("Enter a number (999 to end): "); num = readin.nextInt();; count = 1; sum = 0; while (num != 999 && count < 5) { sum += num; count++; System.out.print("Enter a number (999 to end): "); num = readin.nextInt();; } if (num != 999) sum += num; System.out.println("The sum is " << sum);8. Enter a number (999 to end): 10 Enter a number (999 to end): 20 Enter a number (999 to end): 30 Enter a number (999 to end): 999 The sum is 609. Enter a number (999 to end): 10 Enter a number (999 to end): 20 Enter a number (999 to end): 30 Enter a number (999 to end): 40 Enter a number (999 to end): 50 The sum is 15010. for (i = i ; i <= 9 ; i++) { for (j = 1 ; j <= i ; j++) System.out.print("*"); System.out.println(); }11. for (i = 1 ; i <= 5 ; i++) { for (j = 1 ; j <= 5  i ; j++) System.out.print(" "); for (j = 1 ; j <= 2*i1 ; j++)9/21/2016 CSC161 Intro to CS in Java  Loop Worksheet 2 Answershttp://cs.middlesexcc.edu/~schatz/csc161/worksheets/loop.more.worksheet.ans.html 3/3 for (j = 1 ; j <= 2*i1 ; j++) System.out.print("*"); System.out.println(); }12. System.out.print("Enter y or n: "); ans = readin.nextInt();; while (ans != 'y' && ans != 'n') { System.out.print("Error: please enter y or n: "); ans = readin.nextInt();; }13. do { System.out.print("Enter y or n: "); ans = readin.nextInt();; } while (ans != 'y' && ans != 'n');Email Me | Office Hours | My Home Page | Department Home | MCC Home Page © Copyright Emmi Schatz


View Full Document
Download Loops Worksheet 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Loops Worksheet 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Loops Worksheet 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?