Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

EXPLAIN how to CREATE the TABLES in DEPTH to create PATTERNS with NESTED LOOPS,

ID: 3605769 • Letter: E

Question

EXPLAIN how to CREATE the TABLES in DEPTH to create PATTERNS with NESTED LOOPS, with multiple examples that a show hard and easy ones, and some with whitespace in the patterns, this is in java

to print a pattern using nested loops.
Suggested steps:
1. Start with an outer loop that loops through all the lines
2. Draw the table
3. Use individual transformations to find the expressions for the inner loops
4. Add the inner loop based on the expressions you figured out in step 2 and 3
If you need a single string use a print statement instead of a loop
5. Remember to include the newlines

Explanation / Answer


import java.util.Scanner;
public class Pattern {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("enter number of rows to print pattern:");
int rows=sc.nextInt();
int val=rows;
for(int i=1;i<=rows;i++){
for(int j=1;j<=val;j++){
System.out.print("#");
}
val--;
System.out.println();
}
}
}

output:

enter number of rows to print pattern:
7
#######
######
#####
####
###
##
#

comments;

the above program is one of example for drawing pattern using nested loops:

explanation:

->firstly it asks the user to enter number of rows:rows is nothing but..how many lines you want to print

->next, i declare a integer variable val and stores the value of row in it..val is used for print colum values on console

->now.for loop starts from 1 to given row value

->when i=1

enters into inside loop to print pattern on console.from 1 to val;supose val=7

at first iteration it prints * * * * * * * for seven times and val is decrement to 1 ,now val is 6

->after loop executes it comes out of it..and prints new line

->now i=2 and and enters inside loop

now val =6 ,so inner loop iterates for 1 to 6 and prints * * * * * * (6 times )and decrements by 1 and val becomes 5

and prints new line

->this will continue till i<=rows which means till i becomes 7

//the below one is hard and it prints diferent pattern in same lines:

public class patternexample {

static int n=10;

static int space3=5;

static int val3=1;

static int space2=5;

static int val2=1;

public static void main(String[] args) {

// TODO Auto-generated method stub

for(int i=1;i<=n;i++){

printfirstpattern(i);

}

}

public static void printfirstpattern(int z){

for(int i=z;i<=z;i++){

for(int j=1;j<=n;j++){

if(i<=4)

System.out.print("*");

else

System.out.print(" ");

}

System.out.print(" ");

printsecondpattern(z);

System.out.println(" ");

}

}

public static void printsecondpattern(int z){

for(int i=z;i<=z;i++){

if(i<=5){

for(int j=1;j<=space2;j++){

System.out.print(" ");

}

for(int k=1;k<=val2;k++){

System.out.print("*");

}

space2--;

val2+=2;

System.out.print(" ");

printthirdpattern(z);

//System.out.println(" ");

}else{

//for(int s=1;s<=10;s++){

// System.out.print(" ");

//}

System.out.print(" ");

printthirdpattern(z);

}

}

}

public static void printthirdpattern(int z){

for(int i=z;i<=z;i++){

for(int j=1;j<=space3;j++){

System.out.print(" ");

}

for(int k=1;k<=val3;k++){

System.out.print("*");

}

if(i<=5){

space3--;

val3+=2;

}else{

val3-=2;

space3++;

}

//System.out.println(" ");

}

}

}

output:

********** * *

********** *** ***

********** ***** *****

********** ******* *******

********* *********

***********

*********

*******

*****

***

explanation:

this is hard example for this 3 different patterns i created 3 metods ..based on main loop ..the 3 methods executes for single line...

firstmethod first line followed by second method firstline followed by third method firstline and now it prints new line

next next line of 3 methods so...on till end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote