CSC 1301 Lab #8, nested for loops Part 1. (Simple Practice)(10 credits) Write a program which output will be as below. Make sure your program is scalable (reusable). When parameter n is 5, Output: 1...

1 answer below »

Provide screenshot of each code and output




CSC 1301 Lab #8, nested for loops Part 1. (Simple Practice)(10 credits) Write a program which output will be as below. Make sure your program is scalable (reusable). When parameter n is 5, Output: 1 21 321 4321 54321 When parameter n is 7, pattern to output is 1 21 321 4321 54321 654321 7654321 And so on! The program stays the same, only the value of parameter changes. Part 2. (Simple More Practice)(20 Credits) a) 54321 4321 321 21 1 b) 54321 5432 543 54 5 c) (If else) Example: for(i=1; i<=rows; i++){="" for(j="1;"><=i; j++){="" if(i%2="=1){" system.out.print("0");="" }="" else{="" system.out.print("1");="" }="" }="" system.out.println();="" }="" 0="" 11="" 000="" 1111="" 00000="" d)(if="" else)="" 0="" 10="" 010="" 1010="" 01010="" part="" 3.="" (check="" outputs)(10="" credits)="" show="" the="" output="" of="" following="" statement="" for="" (int="" i="1;" i=""><= 3;="" i++)="" {="" system.out.println("outer="" loop="" i=" + i); for (int j = i; j <= 3; j++) { System.out.println(" \ti=" + i + " j=" + j); } } Output: outer loop i= 1 i= 1 j= 1 i= 1 j= 2 i= 1 j= 3 outer loop i= 2 i= 2 j= 2 i= 2 j= 3 outer loop i= 3 i= 3 j= 3 for (int line = 1; line <= 5; line++) { for (int j = 1; j <= (-1 * line + 5); j++) { System.out.print(" .");="" }="" system.out.println(line);="" }="" output:="" ....1="" ...2="" ..3="" .4="" 5="" int="" n="5;" for="" (int="" line="1;" line=""><= n;="" line++)="" {="" for="" (int="" j="1;" j=""><= (-1="" *="" line="" +="" 5);="" j++)="" {="" system.out.print(".");="" }="" system.out.println(line);="" }="" output:="" ....1="" ...2="" ..3="" .4="" 5="" int="" n="7;" for="" (int="" line="1;" line=""><= n;="" line++)="" {="" for="" (int="" j="1;" j=""><= (-1="" *="" line="" +="" n);="" j++)="" {="" system.out.print(".");="" }="" system.out.println(line);="" }="" output:="" ......1="" .....2="" ....3="" ...4="" ..5="" .6="" 7="" part="" 4.="" (check="" code="" block)(15="" credits)="" find="" the="" output="" of="" following="" code="" block:="" public="" class="" nestedforloop="" {="" public="" static="" final="" int="" n="7;" public="" static="" void="" main(string[]="" args)="" {="" for="" (int="" line="1;" line=""><= n;="" line++)="" {="" for="" (int="" j="1;" j=""><= (-1="" *="" line="" +="" n);="" j++)="" {="" system.out.print("="" ");="" }="" system.out.print(line);="" for="" (int="" i="1;" i=""><= 2="" *="" line="" -="" 1;="" i++)="" {="" system.out.print("="" ");="" }="" system.out.println(line);="" }="" }="" }="" find="" the="" output="" of="" following="" code="" block:="" public="" class="" nestedforloop="" {="" public="" static="" final="" int="" n="6;" public="" static="" void="" main(string[]="" args)="" {="" for="" (int="" line="N;" line="">= 1; line--) { for (int j = 1; j <= (-1="" *="" line="" +="" n);="" j++)="" {="" system.out.print("="" ");="" }="" system.out.print(line);="" for="" (int="" i="1;" i=""><= 2="" *="" line="" -="" 1;="" i++)="" {="" system.out.print("="" ");="" }="" system.out.println(line);="" }="" }="" }="" find="" the="" output="" of="" following="" code="" block:="" public="" class="" nestedforloop="" {="" public="" static="" final="" int="" n="6;" public="" static="" void="" main(string[]="" args)="" {="" for="" (int="" line="N;" line="">= 1; line--) { for (int j = 1; j <= (-1="" *="" line="" +="" n);="" j++)="" {="" system.out.print("="" ");="" }="" system.out.println(line);="" }="" for="" (int="" line="1;" line=""><=n ;="" line++)="" {="" for="" (int="" j="1;" j=""><= (-1 * line + n); j++) { system.out.print(" "); } system.out.println(line); } } } part 5. (difficult practice) (15 credits) write a program to create the following figure by using of nested for loops. please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure. line = 8 +----------------+ | /\ | | /..\ | | /....\ | | /......\ | | /........\ | | /..........\ | | /............\ | |/..............\| |\............../| | \............/ | | \........../ | | \......../ | | \....../ | | \..../ | | \../ | | \/ | +----------------+ line = 4 +--------+ | /\ | | /..\ | | /....\ | |/......\| |\....../| | \..../ | | \../ | | \/ | +--------+ (15 credits) write a program to create the following figure by using of nested for loops. please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure. line = 8 +----------------+ |\............../| | \............/ | | \........../ | | \......../ | | \....../ | | \..../ | | \../ | | \/ | | /\ | | /..\ | | /....\ | | /......\ | | /........\ | | /..........\ | | /............\ | |/..............\| +----------------+ line = 4 +--------+ |\....../| | \..../ | | \../ | | \/ | | /\ | | /..\ | | /....\ | |/......\| +--------+ (15 credits) write a program to create the following figure by using of nested for loops. please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure. segment = 3 and height = 4 * *** ***** ******* * *** ***** ******* * *** ***** ******* segment = 2 and height = 6 * *** ***** ******* ********* *********** * *** ***** ******* ********* *********** (extra 30 credits) write a program to create the following figure by using of nested for loops. please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure. segment = 3 and height = 4 ******* ***** *** * ******* ***** *** * ******* ***** *** * segment = 2 and height = 6 *********** ********* ******* ***** *** * *********** ********* ******* ***** *** * page 9 of 9 (-1="" *="" line="" +="" n);="" j++)="" {="" system.out.print("="" ");="" }="" system.out.println(line);="" }="" }="" }="" part="" 5.="" (difficult="" practice)="" (15="" credits)="" write="" a="" program="" to="" create="" the="" following="" figure="" by="" using="" of="" nested="" for="" loops.="" please="" assign="" the="" number="" of="" lines="" as="" class="" constant="" variable="" in="" order="" to="" change="" the="" size="" of="" figure="" as="" in="" the="" sample="" figure.="" line="8" +----------------+="" |="" \="" |="" |="" ..\="" |="" |="" ....\="" |="" |="" ......\="" |="" |="" ........\="" |="" |="" ..........\="" |="" |="" ............\="" |="" |/..............\|="" |\............../|="" |="" \............/="" |="" |="" \........../="" |="" |="" \......../="" |="" |="" \....../="" |="" |="" \..../="" |="" |="" \../="" |="" |="" \/="" |="" +----------------+="" line="4" +--------+="" |="" \="" |="" |="" ..\="" |="" |="" ....\="" |="" |/......\|="" |\....../|="" |="" \..../="" |="" |="" \../="" |="" |="" \/="" |="" +--------+="" (15="" credits)="" write="" a="" program="" to="" create="" the="" following="" figure="" by="" using="" of="" nested="" for="" loops.="" please="" assign="" the="" number="" of="" lines="" as="" class="" constant="" variable="" in="" order="" to="" change="" the="" size="" of="" figure="" as="" in="" the="" sample="" figure.="" line="8" +----------------+="" |\............../|="" |="" \............/="" |="" |="" \........../="" |="" |="" \......../="" |="" |="" \....../="" |="" |="" \..../="" |="" |="" \../="" |="" |="" \/="" |="" |="" \="" |="" |="" ..\="" |="" |="" ....\="" |="" |="" ......\="" |="" |="" ........\="" |="" |="" ..........\="" |="" |="" ............\="" |="" |/..............\|="" +----------------+="" line="4" +--------+="" |\....../|="" |="" \..../="" |="" |="" \../="" |="" |="" \/="" |="" |="" \="" |="" |="" ..\="" |="" |="" ....\="" |="" |/......\|="" +--------+="" (15="" credits)="" write="" a="" program="" to="" create="" the="" following="" figure="" by="" using="" of="" nested="" for="" loops.="" please="" assign="" the="" number="" of="" lines="" as="" class="" constant="" variable="" in="" order="" to="" change="" the="" size="" of="" figure="" as="" in="" the="" sample="" figure.="" segment="3" and="" height="4" *="" ***="" *****="" *******="" *="" ***="" *****="" *******="" *="" ***="" *****="" *******="" segment="2" and="" height="6" *="" ***="" *****="" *******="" *********="" ***********="" *="" ***="" *****="" *******="" *********="" ***********="" (extra="" 30="" credits)="" write="" a="" program="" to="" create="" the="" following="" figure="" by="" using="" of="" nested="" for="" loops.="" please="" assign="" the="" number="" of="" lines="" as="" class="" constant="" variable="" in="" order="" to="" change="" the="" size="" of="" figure="" as="" in="" the="" sample="" figure.="" segment="3" and="" height="4" *******="" *****="" ***="" *="" *******="" *****="" ***="" *="" *******="" *****="" ***="" *="" segment="2" and="" height="6" ***********="" *********="" *******="" *****="" ***="" *="" ***********="" *********="" *******="" *****="" ***="" *="" page="" 9="" of="">
Answered 1 days AfterMar 05, 2021

Answer To: CSC 1301 Lab #8, nested for loops Part 1. (Simple Practice)(10 credits) Write a program which output...

Pulkit answered on Mar 07 2021
154 Votes
.idea/.gitignore
# Default ignored files
/shelf/
/workspace.xml
.idea/lab 8.iml







.idea/misc.xml




.idea/modules.xml






.idea/workspace.xml






















1615048067695


1615048067695



lab-8problems-1p5dmmxv.docx
CSC 1301
Lab #8, nested for loops
Part 1. (Simple Practice)(10 credits)
Write a program which output will be as below.
Mak
e sure your program is scalable (reusable).
When parameter n is 5,
Output:
1
21
321
4321
54321
When parameter n is 7, pattern to output is
1
21
321
4321
54321
654321
7654321
And so on! The program stays the same, only the value of parameter changes.
Part 2. (Simple More Practice)(20 Credits)
a)
54321
4321
321
21
1
b)
54321
5432
543
54
5
c)
(If else)
Example:
for(i=1; i<=rows; i++){
for(j=1; j<=i; j++){
if(i%2==1){
System.out.print("0");
}
else{
System.out.print("1");
}
}
System.out.println();
}
0
11
000
1111
00000
d)(If else)
0
10
010
1010
01010
Part 3. (Check Outputs)(10 Credits)
Show the output of following statement
    for (int i = 1; i <= 3; i++) {
            System.out.println("outer loop i= " + i);
            for (int j = i; j <= 3; j++) {
                System.out.println("\ti= " + i + " j= " + j);
            }
    }
    
Output:
        outer loop i= 1
    i= 1 j= 1
    i= 1 j= 2
    i= 1 j= 3
outer loop i= 2
    i= 2 j= 2
    i= 2 j= 3
outer loop i= 3
    i= 3 j= 3
    for (int line = 1; line <= 5; line++) {
         for (int j = 1; j <= (-1 * line + 5); j++) {
         System.out.print(".");
         }
         System.out.println(line);
        }
Output:
....1
...2
..3
.4
5
    
    int n = 5;
for (int line = 1; line <= n; line++) {
         for (int j = 1; j <= (-1 * line + 5); j++) {
         System.out.print(".");
         }
         System.out.println(line);
        }
Output:
....1
...2
..3
.4
5
int n = 7;
    for (int line = 1; line <= n; line++) {
        for (int j = 1; j <= (-1 * line + n); j++) {
            System.out.print(".");
        }
        System.out.println(line);
    }
    Output:
......1
.....2
....3
...4
..5
.6
7
Part 4. (Check Code Block)(15 Credits)
Find the output of following code block:
public class NestedForLoop {
    public static final int N = 7;
    public static void main(String[] args) {
        for (int line = 1; line <= N; line++) {
            for (int j = 1; j <= (-1 * line + N); j++) {
                System.out.print(" ");
            }
            System.out.print(line);
            for (int i = 1; i <= 2 * line - 1; i++) {
                System.out.print(" ");
            }
            System.out.println(line);
        }
    }
}
Find the output of following code block:
public class NestedForLoop {
    public static final int N = 6;
    public static void main(String[] args) {
        for (int line = N; line >= 1; line--) {
            for (int j = 1; j <= (-1 * line + N); j++) {
                System.out.print(" ");
            }
            System.out.print(line);
            for (int i = 1; i <= 2 * line - 1; i++) {
                System.out.print(" ");
            }
            System.out.println(line);
        }
    }
}
Find the output of following code block:
public class NestedForLoop {
    public static final int N = 6;
    public static void main(String[] args) {
        for (int line = N; line >= 1; line--) {
            for (int j = 1; j <= (-1 * line + N); j++) {
                System.out.print(" ");
            }
            System.out.println(line);
        }
        for (int line = 1; line <=N ; line++) {
            for (int j = 1; j <= (-1 * line + N); j++) {
                System.out.print(" ");
            }
            System.out.println(line);
        }
    }
}
Part 5. (Difficult Practice)
(15 Credits)
Write a program to create the following figure by using of nested for loops. Please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure.
Line = 8
+----------------+
| /\ |
| /..\ |
| /....\ |
| /......\ |
| /........\ |
| /..........\ |
| /............\ |
|/..............\|
|\............../|
| \............/ |
| \........../ |
| \......../ |
| \....../ |
| \..../ |
| \../ |
| \/ |
+----------------+
Line = 4
+--------+
| /\ |
| /..\ |
| /....\ |
|/......\|
|\....../|
| \..../ |
| \../ |
| \/ |
+--------+
(15 Credits)
Write a program to create the following figure by using of nested for loops. Please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure.
Line = 8
+----------------+
|\............../|
| \............/ |
| \........../ |
| \......../ |
| \....../ |
| \..../ |
| \../ |
| \/ |
| /\ |
| /..\ |
| /....\ |
| /......\ |
| /........\ |
| /..........\ |
| /............\ |
|/..............\|
+----------------+
Line = 4
+--------+
|\....../|
| \..../ |
| \../ |
| \/ |
| /\ |
| /..\ |
| /....\ |
|/......\|
+--------+
(15 Credits)
Write a program to create the following figure by using of nested for loops. Please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure.
SEGMENT = 3 and HEIGHT = 4
*
***
*****
*******
*
***
*****
*******
*
***
*****
*******
SEGMENT = 2 and HEIGHT = 6
*
***
*****
*******
*********
***********
*
***
*****
*******
*********
***********
(Extra 30 Credits)
Write a program to create the following figure by using of nested for loops. Please assign the number of lines as class constant variable in order to change the size of figure as in the sample figure.
SEGMENT = 3 and HEIGHT = 4
*******
*****
***
*
*******
*****
***
*
*******
*****
***
*
SEGMENT = 2 and HEIGHT = 6
***********
*********
*******
*****
***
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here